- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 MNIST 数据集构建一个简单的全连接单隐藏层神经网络。
for epoch in range(training_epoch):
for step in range(total_step):
xs = x_train[step*batch_size:(step+1)*batch_size]
ys = y_train[step*batch_size:(step+1)*batch_size]
grads = grad(xs, ys, W, B)
optimizer.apply_gradients(zip(grads, [W, B]))
W和B是tf.Variable类型:
Input_Dim = 784
H1_NN = 64
w1 = tf.Variable(tf.random.normal([Input_Dim, H1_NN], mean=0.0, stddev=1.), dtype=tf.float32)
b1 = tf.Variable(tf.zeros([H1_NN]), dtype=tf.float32)
Output_Dim = 10
w2 = tf.Variable(tf.random.normal([H1_NN, Output_Dim], mean=0.0, stddev=1.0), dtype=tf.float32)
b2 = tf.Variable(tf.zeros([Output_Dim]), dtype=tf.float32)
W = [w1, w2]
B = [b1, b2]enter code here
错误:
Traceback (most recent call last): File "C:/Users/HSNE_LP22B/PycharmProjects/浙大城市学院/mnist手写数字识别_1隐藏层.py", line 91, in optimizer.apply_gradients( grads_and_vars) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 434, in apply_gradients self._create_slots(var_list) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\adam.py", line 149, in _create_slots self.add_slot(var, 'm') File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 574, in add_slot var_key = _var_key(var) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 1065, in _var_key if var._in_graph_mode: AttributeError: 'list' object has no attribute '_in_graph_mode'
我尝试使用 optimizer.apply_gradients(zip(grads, W+B))
,但这种情况发生了
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [784,64] != values[1].shape = [64,10] [Op:Pack] name: packed
最佳答案
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
total_num = len(train_images)
valid_split = 0.2
train_num = int(total_num*(1-valid_split))
train_x = train_images[:train_num]
train_y = train_labels[:train_num]
valid_x = train_images[train_num:]
valid_y = train_labels[train_num:]
test_x = test_images
test_y = test_labels
train_x = train_x.reshape (-1,784)
valid_x = valid_x.reshape(-1,784)
test_x = test_x.reshape(-1,784)
train_x = tf.cast(train_x/255.0, tf.float32)
valid_x = tf.cast(valid_x/255.0, tf.float32)
test_x = tf.cast(test_x/255.0, tf.float32)
train_y = tf.one_hot(train_y,depth=10)
valid_y = tf.one_hot(valid_y,depth=10)
test_y = tf.one_hot(test_y, depth=10)
Input_Dim = 784
H1_NN = 64
W1 = tf.Variable(tf.random.normal([Input_Dim, H1_NN], mean=0.0, stddev=1.0,
dtype=tf.float32))
B1 = tf.Variable(tf.zeros([H1_NN]), dtype = tf.float32)
Output_Dim = 10
W2 = tf.Variable(tf.random.normal([H1_NN, Output_Dim], mean=0.0, stddev=1.0,
dtype=tf.float32))
B2 = tf.Variable(tf.zeros([Output_Dim]), dtype = tf.float32)
W = [W1, W2]
B = [B1, B2]
def model(x, w, b):
x = tf. matmul(x, w[0]) + b[0]
x = tf.nn.relu(x)
x = tf.matmul(x, w[1]) + b[1]
pred = tf.nn.softmax(x)
return pred
def loss(x, y, w, b):
pred = model(x, w, b)
loss_ = tf.keras.losses.categorical_crossentropy(y_true=y, y_pred=pred)
return tf.reduce_mean(loss_)
training_epochs = 20
batch_size = 50
learning_rate = 0.01
def grad(x, y, w, b):
with tf.GradientTape() as tape:
loss_ = loss(x, y, w, b)
return tape.gradient(loss_, [w[0],b[0],w[1],b[1]])
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
def accuracy (x,y, w, b):
pred = model(x, w, b)
correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(y, 1))
return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
steps = int(train_num / batch_size)
loss_list_train = []
loss_list_valid = []
acc_list_train = []
acc_list_valid = []
for epoch in range(training_epochs):
for step in range(steps):
xs = train_x[step * batch_size:(step + 1) * batch_size]
ys = train_y[step * batch_size:(step + 1) * batch_size]
grads = grad(xs, ys, W, B)
optimizer.apply_gradients(zip(grads, [W[0],B[0],W[1],B[1]]))
loss_train = loss(train_x, train_y, W, B).numpy()
loss_valid = loss(valid_x, valid_y, W, B).numpy()
acc_train = accuracy(train_x, train_y,W,B).numpy()
acc_valid = accuracy(valid_x, valid_y, W, B).numpy()
loss_list_train.append(loss_train)
loss_list_valid.append(loss_valid)
acc_list_train.append(acc_train)
acc_list_valid.append(acc_valid)
print("epoch={:3d}, train_loss={:.4f}, train_acc={:.4f}, val_loss={:.4f}, val_acc=
{:.4f}".format(epoch + 1, loss_train, acc_train, loss_valid, acc_valid))
关于tensorflow2.0 - AttributeError : 'list' object has no attribute '_in_graph_mode' happens in optimizer. apply_gradients(grads_and_vars),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62033607/
在尝试制作一个 RL 代理几天后,我终于成功地创造了它的体验,但是当我尝试训练它时,我得到了这个错误。我已经尽我所能:不同的经验,改变了步骤参数......我只是没有想法。 import pyxinp
我使用 Tensorflow 的 compute_gradients() 和 apply_gradients() 函数进行反向传播。通过打印梯度值,我确实看到梯度正在计算,但在调用 apply_gra
我有以下几行作为程序的一部分: tensor_gradients = optimizer.compute_gradients(cross_entropy) with tf.Session() as s
文档对此不太清楚。我认为通过 opt.compute_gradients(E, [v]) 可以获得的梯度包含每个元素的 ∂E/∂x = g(x) x 是 v 存储的张量。 opt.apply_grad
问题:在 TensorFlow 网络中以最有效的方式获得权重增量的最有效方法是什么? 背景 :我已经按如下方式连接了运算符(operator)(感谢这个 SO question ): self.cos
我对 tensorflow 中优化器的apply_gradients和minimize之间的差异感到困惑。例如, optimizer = tf.train.AdamOptimizer(1e-3) gr
我想使用 AdamOptimizer,但我也想每一步编辑我的渐变。 典型用法如下: train_step = tf.train.AdamOptimizer(learning_rate).minimiz
我在 TensorFlow 2.0 中有一个 UNet 实现。该代码运行良好,没有任何错误,但 custom_loss 的值保持不变。 首先,保存模型的 UNet 类, class UNet( obj
我正在尝试使用 MNIST 数据集构建一个简单的全连接单隐藏层神经网络。 for epoch in range(training_epoch): for step in range(total
我是一名优秀的程序员,十分优秀!