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.
初学者求助!!