m = tf.Variable(0.44)
b = tf.Variable(0.87)
error = 0
for x, y in zip(x_data, y_label):
y_hat = m*x+b
error +=(y-y_label)**2
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.001)
train = optimizer.minimize(error)
AttributeError Traceback(最近一次调用
最后)在()
1个优化器= tf.train.GradientDescentOptimizer(learning_rate = 0.001)
----> 2列= Optimizer.minimize(错误)
C:\ Users \ ASUS
PC \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ training \ optimizer.py
最小化(self,loss,global_step,var_list,gate_gradients,
聚合方法,colocate_gradients_with_ops,名称,grad_loss)
第341章
342 colocate_gradients_with_ops = colocate_gradients_with_ops,
-> 343 grad_loss = grad_loss)
344
345 vars_with_grad = [v for g,如果g不为None,则v在grads_and_vars中]
C:\ Users \ ASUS
PC \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ training \ optimizer.py
在compute_gradients(自我,损失,var_list,gate_gradients,
聚合方法,colocate_gradients_with_ops,grad_loss)
392“ Optimizer.GATE_OP,Optimizer.GATE_GRAPH。不是%s”%
393(gate_gradients)
-> 394个self._assert_valid_dtypes([loss])
395如果grad_loss不为None:
396 self._assert_valid_dtypes([grad_loss])
C:\ Users \ ASUS
PC \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ training \ optimizer.py
在_assert_valid_dtypes中(自身,张量)
第541章
t为542的张量:
-> 543 dtype = t.dtype.base_dtype
544如果dtype不在valid_dtypes中:
第545章
AttributeError:“ numpy.dtype”对象没有属性“ base_dtype”
最佳答案
因为optimizer.minimize
仅接受Tensor变量。
其余代码实际上也不正确,它看起来应该类似于:
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
m = tf.Variable(0.44)
b = tf.Variable(0.87)
y_hat = m * x + b
error = tf.squared_difference(y, y_hat)
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.001)
train = optimizer.minimize(error)
number_of_iterations = 100000
with tf.Session() as sess:
for _ in xrange(0, number_of_iterations):
sess.run(train, {x:x_data, y: y_label}}
#If you want to see the loss:
#loss = sess.run(error,{x:x_data, y: y_label})
#print("Current loss is:" + str(loss))
关于python-3.x - 为什么在Tensorflow中出现此错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47688392/