2.3.1 TensorFlow 常量 tf.constant()

《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记

TensorFlow 常量 tf.constant()

常量一旦初始化后,就不能修改其值, 通过如下函数定义:

tf.constant(
	value,
	dtype=None,
	shape=None,
	name='Const',
	verify_shape=False
	)

举例:

import tensorflow as tf

A = [[1, 2], [3, 4], [5, 6]]
A_tf = tf.constant(A)

with tf.Session() as sess:
    print(sess.run(A_tf))


# 输出为:
# [[1 2]
#  [3 4]
#  [5 6]]


如果指定的shape大于数据的shape,则用数据的最后一个元素填充

import tensorflow as tf

A = [[1, 2], [3, 4], [5, 6]]
A_tf = tf.constant(A, shape=[5, 2], dtype=tf.float32)

with tf.Session() as sess:
    print(sess.run(A_tf))


# 输出为:
# [[1. 2.]
#  [3. 4.]
#  [5. 6.]
#  [6. 6.]
#  [6. 6.]]

你可能感兴趣的:(TensorFlow,tensorflow,python,深度学习,机器学习)