1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import tensorflow as tf
a = tf.constant([1,2],name="a1") b = tf.constant([3,4],name="b1") res = a + b with tf.Session() as sess: c = sess.run(res) print(res.get_shape()) print(sess.run(a)) print(a)
weight_matrix = tf.Variable(tf.random_normal([2, 3], stddev=2))
w1 = tf.Variable([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]) zeros = tf.Variable(tf.zeros([2, 3], tf.int32)) ones = tf.Variable(tf.ones([2,3],tf.int32)) array_define = tf.Variable(tf.constant([1, 2])) constant = tf.Variable(tf.constant([1,2,3]))
bias = tf.Variable(tf.zeros([3])) w2 = tf.Variable(w1.initialized_value()) W3 = tf.Variable(w1.initialized_value() * 2.0)
w4 = tf.Variable(tf.truncated_normal([2,3],stddev=1)) w5 = tf.Variable(tf.random_uniform([2,3],minval=0,maxval=2)) w6 = tf.Variable(tf.random_gamma([2,3],1))
init_op = tf.global_variables_initializer() sess.run(init_op)
print("输出 2*3 权重矩阵") print(sess.run(weight_matrix)) print("w1") print(sess.run(w1)) print("zero") print(sess.run(zeros)) print("ones") print(sess.run(ones)) print("array_define") print(sess.run(array_define)) print("constant") print(sess.run(constant)) print("bias") print(sess.run(bias))
print(sess.run(w4)) print(sess.run(w5)) print(sess.run(w6))
w1 = tf.Variable(tf.random_normal((2,3), stddev=1, seed=1)) w2 = tf.Variable(tf.random_normal((3,1), stddev=1, seed=1))
x = tf.constant([[1.0,2.0]]) a = tf.matmul(x,w1) y = tf.matmul(a,w2)
init_op = tf.global_variables_initializer() sess.run(init_op) print("y:") print(sess.run(y))
x1 = tf.placeholder(tf.float32,shape=(3,2),name="input") a = tf.matmul(x1,w1) y1 = tf.matmul(a,w2) print("y1") print(sess.run(y1,feed_dict={x1:[[1,2],[2,3],[3,4]]}))
|