【发布时间】:2018-01-19 08:38:17
【问题描述】:
我正在尝试运行这个 TensorFlow 示例。我使用的占位符似乎不正确。运行时错误信息对新手没有多大帮助:-)
# Building a neuronal network with TensorFlow
import tensorflow as tf
def multilayer_perceptron( x, weights, biases ):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Output layer with linear activation
out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
return out_layer
session = tf.Session()
nInputs = 7 # Number of inputs to the neuronal network
nHiddenPerceptrons = 5
nTypes = 10 # seven posible types of values in the output
nLearningRate = 0.001
nTrainingEpochs = 15
aInputs = [ [ 1, 1, 1, 0, 1, 1, 1 ], # zero 2
[ 1, 0, 0, 0, 0, 0, 1 ], # one -------
[ 1, 1, 0, 1, 1, 1, 0 ], # two 3 | | 1
[ 1, 1, 0, 1, 0, 1, 1 ], # three | 4 |
[ 1, 0, 1, 1, 0, 0, 1 ], # four -------
[ 0, 1, 1, 1, 0, 1, 1 ], # five | |
[ 0, 1, 1, 1, 1, 1, 1 ], # six 5 | | 7
[ 1, 1, 0, 0, 0, 0, 1 ], # seven -------
[ 1, 1, 1, 1, 1, 1, 1 ], # eight 6
[ 1, 1, 1, 1, 0, 1, 1 ] ] # nine
aOutputs = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
weights = { 'h1': tf.Variable( tf.random_normal( [ nInputs, nHiddenPerceptrons ] ) ),
'out': tf.Variable( tf.random_normal( [ nHiddenPerceptrons, nTypes ] ) ) }
biases = { 'b1': tf.Variable( tf.random_normal( [ nHiddenPerceptrons ] ) ),
'out': tf.Variable( tf.random_normal( [ nTypes ] ) ) }
x = tf.placeholder( "float", shape=[ None,] )
y = tf.placeholder( "float" )
network = multilayer_perceptron( x, weights, biases )
loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( logits=network, labels=tf.placeholder( "float" ) ) )
optimizer = tf.train.AdamOptimizer( learning_rate = nLearningRate ).minimize( loss )
init = tf.global_variables_initializer()
with tf.Session() as session :
session.run( init )
# Training cycle
for epoch in range( nTrainingEpochs ) :
avg_loss = 0.
for n in range( len( aInputs ) ) :
c = session.run( [ optimizer, loss ], { x: aInputs[ n ], y: aOutputs[ n ] } )
# Compute average loss
avg_loss += c / total_batch
print("Epoch:", '%04d' % ( epoch + 1 ), "cost=", "{:.9f}".format( avg_loss ) )
print("Optimization Finished!")
但是我遇到了一些运行时错误,我不知道如何解决它们。感谢您的帮助,谢谢
文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py”,第 671 行,在 _call_cpp_shape_fn_impl input_tensors_as_shapes,状态) 退出中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\contextlib.py”,第 88 行 下一个(self.gen) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\errors_impl.py”,第 466 行,在 raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(状态)) tensorflow.python.framework.errors_impl.InvalidArgumentError:形状必须为 2 级,但对于输入形状为 [?]、[7,5] 的“MatMul”(操作:“MatMul”)为 1 级。 在处理上述异常的过程中,又出现了一个异常: 回溯(最近一次通话最后): 文件“tf_nn.py”,第 42 行,在 网络 = 多层感知器(x,权重,偏差) 多层感知器中的文件“tf_nn.py”,第 7 行 layer_1 = tf.add(tf.matmul(x, weights['h1']), 偏差['b1']) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py”,第 1816 行,在 matmul a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py”,第 1217 行,在 _mat_mul transpose_b=transpose_b,名称=名称) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py”,第 767 行,在 apply_op op_def=op_def) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py”,第 2508 行,在 create_op set_shapes_for_outputs(ret) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py”,第 1873 行,在 set_shapes_for_outputs 形状 = shape_func(op) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py”,第 1823 行,在 call_with_requiring 返回 call_cpp_shape_fn(op, require_shape_fn=True) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py”,第 610 行,在 call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) _call_cpp_shape_fn_impl 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\common_shapes.py”,第 676 行 引发 ValueError(err.message) ValueError:形状必须为 2 级,但对于输入形状为 [?]、[7,5] 的“MatMul”(操作:“MatMul”)为 1 级。
【问题讨论】:
-
如果您希望我们能够提供帮助,您必须向我们提供您看到的错误。
标签: tensorflow