[英]I keep receiving a weird "invalid syntax" error when trying to implement a convolution neural network program using tensorflow in python
I am currently using whatever version of python comes built-in with Google Colab (I believe it is either 3.7, 3.8, or 3.9).我目前使用的是 Google Colab 内置的 python 的任何版本(我相信它是 3.7、3.8 或 3.9)。 I am trying to execute a CNN program that can be used to recognize images without using Keras, but I keep getting a strange error that says this:
我正在尝试执行一个 CNN 程序,该程序可用于在不使用 Keras 的情况下识别图像,但我不断收到一个奇怪的错误消息:
File "<ipython-input-12-6518b14c949f>", line 91
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
^
SyntaxError: invalid syntax
This is the full code that I currently have:这是我目前拥有的完整代码:
!pip install tensorflow_datasets
!pip install --upgrade tensorflow
!pip install tensorflow-datasets
!pip install mnist
#!pip install tensorflow.examples.tutorials.mnist
import argparse
print ('argparse version: ', argparse.__version__)
import mnist
print ('MNIST version: ', mnist.__version__)
import tensorflow_datasets
print ('tensorflow_datasets version: ', tensorflow_datasets.__version__)
import tensorflow.compat.v1 as tf
print ('tf version: ', tf.__version__)
tf.disable_v2_behavior()
#from tensorflow.examples.tutorials.mnist import input_data
#def build_arg_parser():
# parser = argparse.ArgumentParser(description='Build a CNN classifier \
# using MNIST data')
# parser.add_argument('--input-dir', dest='input_dir', type=str,
# default='./mnist_data', help='Directory for storing data')
# return parser
def get_weights(shape):
data = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(data)
def get_biases(shape):
data = tf.constant(0.1, shape=shape)
return tf.Variable(data)
def create_layer(shape):
# Get the weights and biases
W = get_weights(shape)
b = get_biases([shape[-1]])
return W, b
def convolution_2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1],
padding='SAME')
def max_pooling(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
if __name__ == '__main__':
#args = build_arg_parser().parse_args()
# Get the MNIST data
mnist = tensorflow_datasets.load('mnist')
# The images are 28x28, so create the input layer
# with 784 neurons (28x28=784)
x = tf.placeholder(tf.float32, [None, 784])
# Reshape 'x' into a 4D tensor
x_image = tf.reshape(x, [-1, 28, 28, 1])
# Define the first convolutional layer
W_conv1, b_conv1 = create_layer([5, 5, 1, 32])
# Convolve the image with weight tensor, add the
# bias, and then apply the ReLU function
h_conv1 = tf.nn.relu(convolution_2d(x_image, W_conv1) + b_conv1)
# Apply the max pooling operator
h_pool1 = max_pooling(h_conv1)
# Define the second convolutional layer
W_conv2, b_conv2 = create_layer([5, 5, 32, 64])
# Convolve the output of previous layer with the
# weight tensor, add the bias, and then apply
# the ReLU function
h_conv2 = tf.nn.relu(convolution_2d(h_pool1, W_conv2) + b_conv2)
# Apply the max pooling operator
h_pool2 = max_pooling(h_conv2)
# Define the fully connected layer
W_fc1, b_fc1 = create_layer([7 * 7 * 64, 1024])
# Reshape the output of the previous layer
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
# Multiply the output of previous layer by the
# weight tensor, add the bias, and then apply
# the ReLU function
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# Define the dropout layer using a probability placeholder
# for all the neurons
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# Define the readout layer (output layer)
W_fc2, b_fc2 = create_layer([1024, 10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
# Define the entropy loss and the optimizer
y_loss = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_loss))
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
# Define the accuracy computation
predicted = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_loss, 1))
accuracy = tf.reduce_mean(tf.cast(predicted, tf.float32))
# Create and run a session
sess = tf.InteractiveSession()
init = tf.initialize_all_variables()
sess.run(init)
# Start training
num_iterations = 21000
batch_size = 75
print('\nTraining the model....')
for i in range(num_iterations):
# Get the next batch of images
batch = mnist.train.next_batch(batch_size)
# Print progress
if i % 50 == 0:
cur_accuracy = accuracy.eval(feed_dict = {
x: batch[0], y_loss: batch[1], keep_prob: 1.0})
print('Iteration', i, ', Accuracy =', cur_accuracy)
Train on the current batch
optimizer.run(feed_dict = {x: batch[0], y_loss: batch[1], keep_prob: 0.5})
# Compute accuracy using test data
print('Test accuracy =', accuracy.eval(feed_dict = {
x: mnist.test.images, y_loss: mnist.test.labels,
keep_prob: 1.0}))
Originally, I had an error with the line最初,我的行有错误
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_loss))
but after I looked it up, I found that switching it to be但是在我查找之后,我发现将其切换为
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_conv, logits=y_)
would fix that error, and I believe it did.会修复那个错误,我相信它确实做到了。
My next error (and the one that I cannot figure out) came with the line right under that我的下一个错误(也是我无法弄清楚的错误)出现在该行的正下方
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
which is where the invalid syntax error came up.这是出现无效语法错误的地方。 I tried switching it to use a different type of optimizer such as
我尝试将其切换为使用不同类型的优化器,例如
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
but that hasn't seemed to work.但这似乎没有用。 I have found a few sources that use the exact same code, but seem to somehow have it running - I just can't figure out how.
我找到了一些使用完全相同代码的资源,但似乎以某种方式运行了它——我只是不知道如何运行。
I also noticed that no matter what I do, I am also not able to install or use tensorflow.examples.tutorials.mnist
我还注意到,无论我做什么,我也无法安装或使用
tensorflow.examples.tutorials.mnist
Does anyone have any ideas on how to properly install/use tensorflow.examples.tutorials.mnist or on how to fix the error with the AdamOptimizer line?有没有人对如何正确安装/使用 tensorflow.examples.tutorials.mnist 或如何修复 AdamOptimizer 行的错误有任何想法? So far I still haven't been able to find anything that shows a method of fixing these issues that actually works for me.
到目前为止,我仍然无法找到任何显示解决这些问题的方法的方法,这些方法实际上对我有用。 Thank you!
谢谢!
UPDATE: Some nice people let me know that it was actually just a problem with my parentheses.更新:一些好人让我知道这实际上只是我括号的问题。 I added the missing one and fixed another error - the updated section is now this:
我添加了丢失的一个并修复了另一个错误 - 更新的部分现在是这样的:
# Define the entropy loss and the optimizer
y_loss = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_conv, logits=y_loss))
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
However, now I get the following error with the same optimizer line as before:但是,现在我在与以前相同的优化器行中收到以下错误:
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'Variable:0' shape=(5, 5, 1, 32) dtype=float32_ref>", "<tf.Variable 'Variable_1:0' shape=(32,) dtype=float32_ref>", "<tf.Variable 'Variable_2:0' shape=(5, 5, 32, 64) dtype=float32_ref>", "<tf.Variable 'Variable_3:0' shape=(64,) dtype=float32_ref>", "<tf.Variable 'Variable_4:0' shape=(3136, 1024) dtype=float32_ref>", "<tf.Variable 'Variable_5:0' shape=(1024,) dtype=float32_ref>", "<tf.Variable 'Variable_6:0' shape=(1024, 10) dtype=float32_ref>", "<tf.Variable 'Variable_7:0' shape=(10,) dtype=float32_ref>", "<tf.Variable 'Variable_8:0' shape=(5, 5, 1, 32) dtype=float32_ref>", "<tf.Variable 'Variable_9:0' shape=(32,) dtype=float32_ref>", "<tf.Variable 'Variable_10:0' shape=(5, 5, 32, 64) dtype=float32_ref>", "<tf.Variable 'Variable_11:0' shape=(64,) dtype=float32_ref>", "<tf.Variable 'Variable_12:0' shape=(3136, 1024) dtype=float32_ref>", "<tf.Variable 'Variable_13:0' shape=(1024,) dtype=float32_ref>", "<tf.Variable 'Variable_14:0' shape=(1024, 10) dtype=float32_ref>", "<tf.Variable 'Variable_15:0' shape=(10,) dtype=float32_ref>"] and loss Tensor("Mean:0", shape=(), dtype=float32).
Any ideas on how to fix this new error?关于如何解决这个新错误的任何想法? I am very new to tensorflow and CNNs, so I'm not sure what "No gradients provided for any variable" is supposed to point to.
我对 tensorflow 和 CNN 很陌生,所以我不确定“没有为任何变量提供梯度”应该指向什么。
For anyone else that runs into any similar problems, this is what I ended up doing:对于遇到任何类似问题的任何其他人,这就是我最终做的事情:
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_conv, logits=y_loss))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_conv, logits=y_loss))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_loss, logits=y_conv))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_loss, logits=y_conv))
Now it works fine and the whole section now looks like this:现在它工作正常,整个部分现在看起来像这样:
y_loss = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_loss, logits=y_conv))
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.