【问题标题】:got TypeError when running linear regression with tensorflow. ' Op has type float64 that does not match type float32 of argument'使用 tensorflow 运行线性回归时出现 TypeError。 ' Op 的 float64 类型与参数的 float32 类型不匹配'
【发布时间】:2016-05-31 19:59:35
【问题描述】:

我对 tensorflow 还是很陌生。我用张量流做了一个线性回归。 当我运行下面的代码时,我得到了这样的 typeError:

TypeError: 'Mul' Op 的输入 'y' 的 float64 类型与参数 'x' 的 float32 类型不匹配。

花了几个小时,但无法弄清楚原因。 哪里出错了?非常感谢您的帮助。多谢。

import tensorflow as tf
import numpy as np

training_epoch = 1000
display_epoch=50
learning_rate = 0.01
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
X = tf.placeholder('float')
Y= tf.placeholder ('float')
w = tf.Variable(np.random.randn(2))
pred = tf.add(tf.mul(X,w[0]), w[1])

loss = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
init = tf.initialize_all_variables()
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

with tf.Session() as session:
    session.run(init)
    for epoch in range(training_epoch):
        for x, y in zip(train_X, train_Y):
            session.run(optimizer, feed_dict={X:x, Y:y})
        if (epoch+1) % display_epoch == 0:
            weight = session.run(w)
            bias = session.run(b)
            cost = session.run(loss, feed_dict={X:train_X, Y:train_Y})
            print('epoch: {0:.2f}, weight: {1:.9f}. bias: {2:.9f}, cost: {3:.9f}'.format(epoch+1,weight[0], weight[1], cost))
    print('optimization complete')

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    TL;DR:占位符X和变量w有不同的元素类型,TensorFlow不会自动转换操作参数,所以tf.mul()操作失败。

    您的占位符X 具有tf.float32 类型,因为它被定义为具有dtype 'float',在此行中定义为“32 位浮点”:

    X = tf.placeholder('float')
    

    您的变量w 的类型为tf.float64,因为它是从np.random.randn(2) 初始化的,它的dtype 为np.float64,在这一行中:

    w = tf.Variable(np.random.randn(2))
    

    最简单的解决方案是将w 定义为具有tf.float32 类型:

    w = tf.Variable(np.random.randn(2).astype(np.float32))
    

    或者,您可以将X 定义为具有tf.float64 类型:

    X = tf.placeholder(tf.float64)
    

    还有一个 tf.cast() 用于进行显式转换的操作,但我不建议使用它,因为它不可微分,因此会干扰计算梯度。


    PS。一种更惯用的方法是使用tf.random_normal() op,它可以避免在图中放置大常量:

    w = tf.Variable(tf.random_normal([2]))
    

    虽然对于小变量(比如这里的 2 元素向量)来说并不重要,但对于更大的权重矩阵来说,它变得更加重要。

    【讨论】:

    • 非常感谢您的详细解释!
    猜你喜欢
    • 2016-07-12
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2021-01-06
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多