import tensorflow as tf
import numpy as np
# tf.one_hot()函数将待转换数据,转换为one-hot形式的数据输出
# tf.ont_hot(带转换数据, depth=几分类)
classes = 3
labels = tf.constant([1,0,2])
output = tf.one_hot(labels, depth=classes)
print(output)
>> tf.Tensor(
[[0. 1. 0.][1. 0. 0.][0. 0. 1.]], shape=(3, 3), dtype=float32)
# 进行softmax运算
y = tf.constant([1.01,2.01,-0.66])
y_pro = tf.nn.softmax(y)
print("after softmax, y_pro is",y_pro)
>> after softmax, y_pro is tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)
# 赋值操作,更新参数的值并返回
# 调用assign_sub之前,先用tf.Variable定义变量w为可训练
# w.assign(w要自减的内容)
w = tf.Variable(4)
w.assign_sub(1)
print(w)
>> <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>