tf.placeholder()
def placeholder(dtype, shape=None, name=None):
tf.placeholder()用来传入真是训练样本/测试/真实特征/待处理特征,仅占位,
不必给初值,用sess.run的feed_dict参数以字典的形式喂入x
no.save() / np.load()
def save(file, arr, allow_pickle=True, fix_imports=True):
def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
encoding='ASCII'):
使用示例
import numpy as np
A = np.arange(15).reshape(3,5)
print(A)
np.save("A",A)
B = np.load('A.npy')
print(B)
运行结果
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
tf.shape()
返回数据的维度
import tensorflow as tf
import os
import numpy as np
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # 忽略tensorflow警告信息
x = tf.constant([[1,2,3],[4,5,6]])
A = [[1,3,4],[4,5,6],[7,8,9]]
y =np.arange(12).reshape(1,3,4)
with tf.Session() as sess:
print('A:',sess.run(tf.shape(A)))
print('x:',sess.run(tf.shape(x)))
print('y:',sess.run(tf.shape(y)))
结果:
A: [3 3]
x: [2 3]
y: [1 3 4]
其他
这里演示一下 tf.reshape()
import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # 忽略tensorflow警告信息
x = tf.constant([[1,2,3],[4,5,6]])
a = tf.reshape(x,[2,-1]) # -1表示跟随另一列自动补全,自动计算
b = tf.reshape(x,[-1,2])
c = tf.reshape(x,[1,6])
with tf.Session() as sess:
print('a:',sess.run(tf.shape(a)))
print('b:',sess.run(tf.shape(b)))
print('c:',sess.run(tf.shape(c)))
运行结果
a: [2 3]
b: [3 2]
c: [1 6]