TensorFlow记录(一)

记录学习TensorFlow过程中遇到知识点

1.函数 tf.contrib.keras.preprocessing.sequence.pad_sequences()

用于填充数组(列表),返回一个数组类型。函数参数:

def pad_sequences(sequences: {__iter__, __len__},
                  maxlen: Any = None,
                  dtype: str = 'int32',
                  padding: str = 'pre',
                  truncating: str = 'pre',
                  value: float = 0.) -> ndarray

传入的sequence是一个序列,maxlen是填充的最大长度,padding有两种参数pre , post,分别指定在序列之前或者最后进行填充。truncating指定当最大长度少于输入序列长度时候从前面截断还是从后面截断。最后的value指定填充所用的数字。例如:

data = [[1,2,3.4],[3,4.7,5,6]]

print(type(data))
pad_data = kr.preprocessing.sequence.pad_sequences(data, maxlen=3, dtype=np.float32, truncating='pre')
print(type(pad_data))
print(pad_data)

输出:

<class 'list'>
<class 'numpy.ndarray'>
[[1.  2.  3.4]
 [4.7 5.  6. ]]
2.函数 tf.contrib.keras.utils.to_categorical()

函数将数字转化为对应的 one-hot 编码,函数参数:

def to_categorical(y: Any,
                   num_classes: Any = None,
                   dtype: str = 'float32') -> ndarray

num_class 指定编码的长度,例如:

labels = list(range(4))
labels = kr.utils.to_categorical(labels, num_classes=len(labels))

print(labels)

输出:

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
3.tf.flags.DEFINE_XXX()

例如:

import tensorflow as tf

FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_integer('test_int', 100, 'introduce')

print(FLAGS.test_int)

使用这个函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值