models.
搭建模型 ------ 配置训练方法 ------ 执行训练过程 ------ 统计参数数目
Sequential() compile(). fit() summary()
tf 的 shape
aaa = tf.random_uniform([2,3],minval=-0.1,maxval = 0.1)
print(aaa.shape) # --- 1
with tf.Session() as sess:
print(aaa.get_shape()) # --- 2
输出:
(2, 3) # --- 1
(2, 3) # --- 2
numpy 的shape:
tf.placeholder()
a=tf.placeholder(tf.int32, shape=[2,2])
with tf.Session() as sess:
print(sess.run(a,feed_dict={a:[[2,4],[3,3]]}))
输出:
[[2 4]
[3 3]]
bb = tf.placeholder(tf.int32, shape=[2, ])
cc = tf.placeholder(tf.int32, shape=[2])
with tf.Session() as sess:
print(sess.run(b, feed_dict={b: [2,2] }))
print(sess.run(c, feed_dict={c: [2, 2] }))
输出:
bb = [2 2]
cc = [2 2]
b = tf.placeholder(tf.int32, shape=[None, None])
c = tf.placeholder(tf.int32, shape=[2,])
d = tf.placeholder(tf.float32, shape=[None,1])
e = tf.placeholder(tf.float32, shape=[1, None])
print(b.shape) # (?, ?)
print(c.shape) # (2,)
print(d.shape) # (?, 1)
print(e.shape) # (1, ?)
with tf.Session() as sess:
print(sess.run(b,feed_dict={b:[[4,4,2],[3,5,6],[5,6,7]]}))
print(sess.run(c,feed_dict={c:[42,33] }))
print(sess.run(d,feed_dict={d:[[42],[4],[6]] }))
print(sess.run(e, feed_dict={e:[[42,4,5,6]] }))
输出:
b = [[4 4 2]
[3 5 6]
[5 6 7]]
c = [42 33]
d = [[42.]
[ 4.]
[ 6.]]
e = [[42. 4. 5. 6.]]
矩阵None的用法:对应维度加一维
在 ndarray 和 tensor 类型使用,list 不适用(会报错)
b=np.array([[3,4,5,6],[7,8,9,10],[11,2,3,33]])
print(b) # --- 1
print(b[:,]) # --- 1
print(b[:,None]) # --- 2
print(b.shape) # (3, 4)
print(b[:,None].shape) # (3, 1, 4)
print(b[None,:].shape) # (1, 3, 4)
输出:
# b 和 b[:,] # --- 1
[[ 3 4 5 6]
[ 7 8 9 10]
[11 2 3 33]]
# b[:,None] # --- 2
[[[ 3 4 5 6]]
[[ 7 8 9 10]]
[[11 2 3 33]]]