记录学习TensorFlow过程中遇到知识点
文章目录
-
-
-
- 1.函数 tf.contrib.keras.preprocessing.sequence.pad_sequences()
- 2.函数 tf.contrib.keras.utils.to_categorical()
- 3.tf.flags.DEFINE_XXX()
- 4.tf.contrib 快速处理文本数据
- 5.文本数据随机打乱方法
- 6. tf.concat() 函数-拼接数组
- 7.tf.contrib.layers.l2_regularizer()
- 8.tf.nn.embedding_lookup()函数
- 9.tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=Y)
- 10.关于 one-hot 数据的生成
-
-
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)
使用这个函数