tensorflow入门第一讲
NameError: name 'shape' is not defined
解决:
from tensorflow import shape
错误:
TypeError: 'function' object is not subscriptable
解决:
对应函数代码小括号不完整,仔细检查即可
错误:
w2 = tf.Variable(tf.constant([[0.2,0.1,0.3],[0.2,0.4,0.3]]),name="w2")
^
SyntaxError: invalid syntax
解决:
shape后面应该跟等号,而不是小括号
附上全部可正常运行的代码:
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
from tensorflow import shape
x = tf.constant([0.9,0.85], shape=[1,2])
w1 = tf.Variable(tf.constant([[0.2,0.1,0.3],
[0.2,0.4,0.3]],
shape=[2,3]),name="w1")
w2 = tf.Variable(tf.constant([0.2,0.5,0.25],
shape=[3,1]),name="w2")
b1 = tf.constant([-0.3,0.1,0.2],
shape=[1,3], name="b1")
b2 = tf.constant([-0.3], shape=[1],name="b2")
init_op=tf.global_variables_initializer()
a=tf.matmul(x ,w1)+b1
y=tf.matmul(a ,w2)+b2
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(y))
错误:
NameError: name 'w2' is not defined
解决:
原因是少写了w2对应的一行代码,补上即可
附正确运行第二段代码:
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
x = tf.constant([0.9,0.85], shape=[1,2])
w1 = tf.Variable(tf.random_normal([2,3],
stddev=1, seed=1),name="w1")
w2 = tf.Variable(tf.random_normal([3,1],
stddev=1, seed=1),name="w1")
b1 = tf.Variable(tf.zeros([1,3]))
b2 = tf.Variable(tf.ones([1]))
init_op=tf.global_variables_initializer()
a = tf.matmul(x ,w1)+b1
y = tf.matmul(a, w2)+b2
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(y))
以上是前馈神经网络算法练习。