TensorFlow

1、创建常量

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

value是必须设置的,dtype是数据类型(tf.float32、tf.float64),shape格式(参考矩阵),verify_shape是否检查value的形状与shape相符合。

2、初始化模型

with tf.Session() as sess:
    tf.global_variables_initializer().run()

3、乘法

tf.multiply()与tf.matmul()的区别

前者将矩阵中的相应元素相乘。

[En]

The former multiplies the corresponding elements in the matrix.

后者是进行矩阵乘法

4、feed_dict

赋值和传递参数(个人理解,估计不严谨)

[En]

Assign values and transfer parameters (personal understanding, it is estimated to be not rigorous)

import tensorflow as tf
str = tf.compat.v1.placeholder(tf.string)
with tf.compat.v1.Session() as sess:
    output = sess.run(str, feed_dict={str:'Hello CSDN'})
    print(output)
Num1 = tf.compat.v1.placeholder(tf.float32)
Num2 = tf.compat.v1.placeholder(tf.float32)

Result = tf.multiply(Num1, Num2)

with tf.compat.v1.Session() as sess:
    print(sess.run(Result, feed_dict={Num1:[5.],Num2:[6.]}))

5、tf.one_hot()

tf.one_hot(
    indices,
    depth,
    on_value=None,
    off_value=None,
    axis=None,
    dtype=None,
    name=None
)

indices:将on_value值填入indices描述的位置中,其余用off_value填充

depth:输出时最内层的元素个数(axis默认时)

axis:假设输出result

默认为-1时,输出为indices.shape()*depth格式,其中将result[0][indices[0]]、result[1][indices[1]]…..等位置的值设置为on_value,其余设置为off_value

设置为0时,输出为depth*indices.shape()的格式,其中将depth[indices[0]][0]、result[indices[1]][1]…等位置的值设置为on_value,其余设置为off_value

6、np.eye(n)[x]

没有x参数时,生成一个n*n的单位矩阵

有x参数时,生成n*x.shape[1]的矩阵(也有可能是数组),其中result[0][x[0]]、result[1][x[1]]…..等位置的值设置为1,其余位置为0。

7、tf.reduce_mean()

reduce_mean(input_tensor,
                axis=None,
                keep_dims=False,
                name=None)

input_tensor:进行计算的原型

axis:

如果未设置,则默认情况下计算所有元素的平均值

[En]

When not set, the mean of all elements is calculated by default

设置为0时,保留input_tensor[0]维度的情况下计算均值

设置为1时, 保留input_tensor[1]维度的情况下计算均值

keep_dims:设置是否保留原有维度,True为保持,False为不报持

8、tf.nn.softmax_cross_entropy_with_logits()

计算使用了softmax函数对应的损失函数的最小值

三个参数logits是最后一层的输出,lables是原有的结果Y,name无所谓

Original: https://blog.csdn.net/weixin_42531723/article/details/122088700
Author: 卑微康康
Title: TensorFlow

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/513910/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球