Machine learning 没有为任何变量提供梯度
我有一个小问题:Machine learning 没有为任何变量提供梯度,machine-learning,tensorflow,Machine Learning,Tensorflow,我有一个小问题: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt Nclass = 500 D = 2 M = 3 K = 3 X1 = np.random.randn(Nclass, D) + np.array([0, -2]) X2 = np.random.randn(Nclass, D) + np.array([2, 2]) X3 = np.random.randn(Nclass, D
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Nclass = 500
D = 2
M = 3
K = 3
X1 = np.random.randn(Nclass, D) + np.array([0, -2])
X2 = np.random.randn(Nclass, D) + np.array([2, 2])
X3 = np.random.randn(Nclass, D) + np.array([-2, 2])
X = np.vstack ([X1, X2, X3]).astype(np.float32)
Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)
plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5)
plt.show()
N = len(Y)
T = np.zeros((N, K))
for i in range(N):
T[i, Y[i]] = 1
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def forward(X, W1, b1, W2, b2):
Z = tf.nn.sigmoid(tf.matmul(X, W1) + b1)
return tf.matmul(Z, W2) + b2
tfX = tf.placeholder(tf.float32, [None, D])
tfY = tf.placeholder(tf.float32, [None, K])
W1 = init_weights([D, M])
b1 = init_weights([M])
W2 = init_weights([M, K])
b2 = init_weights([K])
py_x = forward(tfX, W1, b1, W2, b2)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=py_x, logits=T))
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
predict_op = tf.argmax(py_x, 1)
sess = tf.Session()
inti = tf.initizalize_all_variables()
for i in range(1000):
sess.run(train_op, feed_dict={tfX: X, tfY: T})
pred = sess.run(predict_op, feed_dict={tfX: X, tfY: T})
if i % 10 == 0:
print(np.mean(Y == pred))
回溯(最近一次呼叫最后一次):
文件“test.py”,第45行,在
列车运行=tf.列车梯度降尘器(0.05).最小化(成本)
文件“/usr/local/lib/python3.5/dist packages/tensorflow/python/training/optimizer.py”,第322行
([str(v)代表v,在梯度和变量中为v],损失))
ValueError:没有为任何变量提供渐变,请检查图形中是否有不支持渐变的操作,在变量[“”、“”、“”、“”、“”]和损失张量(“平均值:0”,形状=(),数据类型=浮点64)之间。
不清楚我在这里要做什么。在这一点上,有人能帮我吗?如果
T
是真正的标签,而py_x
是网络输出,则必须切换交叉熵函数中的参数:
Traceback (most recent call last):
File "test.py", line 45, in <module>
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 322, in minimize
([str(v) for _, v in grads_and_vars], loss))
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32_ref>", "<tf.Variable 'Variable_1:0' shape=(3,) dtype=float32_ref>", "<tf.Variable 'Variable_2:0' shape=(3, 3) dtype=float32_ref>", "<tf.Variable 'Variable_3:0' shape=(3,) dtype=float32_ref>"] and loss Tensor("Mean:0", shape=(), dtype=float64).
登录必须是网络输出,标签必须是真实标签。如果混淆参数,优化器将无法反向传播,因为没有梯度。
您还必须在培训前初始化变量;您的代码缺少sess.run(init)语句(您的initialize\u all_variables()
中也有一个输入错误)。
我还洗牌了你的数据;也许这会导致更快地向标签靠拢
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=T, logits=py_x))
它发现你应该运行
inti
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Nclass = 500
D = 2
M = 3
K = 3
X1 = np.random.randn(Nclass, D) + np.array([0, -2])
X2 = np.random.randn(Nclass, D) + np.array([2, 2])
X3 = np.random.randn(Nclass, D) + np.array([-2, 2])
X = np.vstack ([X1, X2, X3]).astype(np.float32)
Y = np.array([0]*Nclass + [1]*Nclass + [2]*Nclass)
perm = np.random.permutation(len(X))
X = X[perm]
Y = Y[perm]
# plt.scatter(X[:,0], X[:,1], c=Y, s=100, alpha=0.5)
# plt.show()
N = len(Y)
T = np.zeros((N, K))
for i in range(N):
T[i, Y[i]] = 1
print(T)
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def forward(X, W1, b1, W2, b2):
Z = tf.nn.sigmoid(tf.matmul(X, W1) + b1)
return tf.matmul(Z, W2) + b2
tfX = tf.placeholder(tf.float32, [None, D])
tfY = tf.placeholder(tf.float32, [None, K])
W1 = init_weights([D, M])
b1 = init_weights([M])
W2 = init_weights([M, K])
b2 = init_weights([K])
py_x = forward(tfX, W1, b1, W2, b2)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=T, logits=py_x))
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(cost)
predict_op = tf.argmax(py_x, 1)
sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
for i in range(1000):
sess.run(train_op, feed_dict={tfX: X, tfY: T})
pred = sess.run(predict_op, feed_dict={tfX: X, tfY: T})
if i % 10 == 0:
print(np.mean(Y == pred))
在运行GradientDescentOptimizer
inti = tf.initialize_all_variables()
sess.run(inti)