Embedding初始化
pytorch: Embedding()
tf2.0: random.normal()
# 验证均值和方差
def confirm(weight):mean = np.sum(weight) / dimprint("均值: {}".format(mean))square_sum = np.sum((mean - weight) ** 2)print("方差: {}".format(square_sum / dim))dim = 1000000 # dim越大,均值、方差越接近0和1embd = nn.Embedding(5, dim) # 默认 训练参数服从(0,1)正态分布
weight = embd.weight.data[0].numpy()
confirm(weight)embd2 = tf.Variable(tf.random.normal([5, dim])) # 设置为(0,1)正态分布
weight2 = embd2.numpy()[0]
confirm(weight2)
张量初始化
pytorch: xavier_uniform_()
tf2.0: GlorotUniform()
def confirm(weight):mean = np.sum(weight) / dimprint("均值: {}".format(mean))square_sum = np.sum((mean - weight) ** 2)print("方差: {}".format(square_sum / dim))dim = 1000000 w = nn.Parameter(torch.zeros(size=(3, dim)))
nn.init.xavier_uniform_(w.data)
weight = w.data[0].numpy()
confirm(weight)initializer = tf.initializers.GlorotUniform()
w2 = tf.Variable(initializer(shape=[3, dim]))
weight2 = w2[0].numpy()
confirm(weight2)
多分类交叉熵损失
pytorch: CrossEntropyLoss()
tf2.0: categorical_crossentropy()
input = np.random.random((3,3))
input_p = torch.tensor(input)
input_t = tf.convert_to_tensor(input)target_p = torch.tensor([1,2,2])
target_t1 = tf.keras.utils.to_categorical([1,2,2])
target_t2 = tf.constant([1,2,2])
target_t3 = tf.one_hot([1,2,2], depth=3)p_f = torch.nn.CrossEntropyLoss()
loss1 = p_f(input_p,target_p)
print(loss1)# 方法一
loss2 = tf.losses.categorical_crossentropy(y_true=target_t1,y_pred=tf.nn.softmax(input_t,axis=1))
print(tf.reduce_mean(loss2))# 方法二
loss3 = tf.keras.losses.sparse_categorical_crossentropy(y_true=target_t2, y_pred=tf.nn.softmax(input_t,axis=1))
print(tf.reduce_mean(loss3))# 方法三
loss4 = tf.keras.losses.categorical_crossentropy(y_true=target_t3, y_pred=tf.nn.softmax(input_t,axis=1))
print(tf.reduce_mean(loss4))
二分类交叉熵损失
pytorch: BCEWithLogitsLoss()
tf2.0: sigmoid_cross_entropy_with_logits()
input = np.random.random((3,3))
input_p = torch.tensor(input)
input_t = tf.convert_to_tensor(input)target = np.array([[0.,1.,1.],[0.,0.,1.],[1.,0.,1.]])
target_p = torch.tensor(target)
target_t = tf.convert_to_tensor(target)p_f = torch.nn.BCEWithLogitsLoss()
loss1 = p_f(input_p,target_p)
print(loss1)# 方法一
loss2 = tf.nn.sigmoid_cross_entropy_with_logits(logits=input_t, labels=target_t)
print(tf.reduce_mean(loss2))# 方法二
loss_fn = tf.keras.losses.BinaryCrossentropy(from_logits=True)
loss3 = loss_fn(y_true=target_t, y_pred=input_t)
print(tf.reduce_mean(loss3))