BeyondTheBestX 2019-07-16 16:05 采纳率: 0%
浏览 1151

初学Tensorflow搭建简单的神经网络,无法理解报错,求助!

import tensorflow as tf
import numpy as np
def add_layer(input_data,input_size,output_size,activation_function=None):
Weights=tf.Variable(tf.random.normal([input_size,output_size]))
biases=tf.Variable(tf.zeros(None,output_size)+0.1)
Wx_plus_biases=tf.matmul(input_data,Weights)
if activation_function==None:
output=Wx_plus_biases
else:
output=activation_function(output)
return output
x_data=np.linspace(-1,1,300)[:,np.newaxis]
noise=np.random.normal(0.0,0.05,x_data)
y_data=np.square(x_data)-0.5+noise
xs=tf.compat.v1.placeholder(tf.float32,[None,1])
ys=tf.compat.v1.placeholder(tf.float32,[None,1])
l1=add_layer(x_data,1,10,activation_function=tf.nn.relu)
prediction=add_layer(l1,10,1,activation_function=None)
loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-precdition),
reduction_indices=[1]))
train_op=tf.train.GradientDicentOptimizer(0.1).minize(loss)
init=tf.global_variables_initializer()

for i in range(1000):
sess.run(train_op,feed_dict={xs:x_data,ys:y_data})
if i%50:
print (sess.run(loss,feed_dict={xs:x_data,ys:y_data}))

以上是源码,下面是报错
Traceback (most recent call last):
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1877, in zeros
    tensor_shape.TensorShape(shape))
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 326, in _tensor_shape_tensor_conversion_function
    "Cannot convert a partially known TensorShape to a Tensor: %s" % s)
ValueError: Cannot convert a partially known TensorShape to a Tensor: <unknown>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Easy_network.py", line 17, in <module>
    l1=add_layer(x_data,1,10,activation_function=tf.nn.relu)
  File "Easy_network.py", line 5, in add_layer
    biases=tf.Variable(tf.zeros(None,output_size)+0.1)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1880, in zeros
    shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1087, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1145, in convert_to_tensor_v2
    as_ref=False)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 305, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 246, in constant
    allow_broadcast=True)
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 284, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/home/vdarkknightx/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 454, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.
初学者求助!!
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-07-16 18:18
    关注

    可能是由于get_shape()返回的是元组,tf.shape()返回的是tensor,所以tf.shape()返回的为None的batch_size维度可以继续作为其他tensor的维度,而get_shape()由于返回的是元组,取到的batch_size维度直接是None了,无法作为其他tensor的维度。

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码