博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
keras、tf、numpy实现logloss对比
阅读量:2135 次
发布时间:2019-04-30

本文共 2725 字,大约阅读时间需要 9 分钟。

文章目录

原理

l o g l o s s = 1 n ∑ [ − y l o g y ^ − ( 1 − y ) l o g ( 1 − y ^ ) ] logloss=\frac{1}{n}\sum[-ylog\hat{y}-(1-y)log(1-\hat{y})] logloss=n1[ylogy^(1y)log(1y^)]

实现

init

import tensorflow as tfimport numpy as npfrom tensorlfow import kerasfrom tensorflow.keras import Sequential, Modelfrom tensorflow.keras.layers import Dense, Input, LSTMtf.random.set_seed(1)rows = 10columns = 3epsilon = 1e-7	# sklearn keras 源码都有epsilon,why?防止log(0)出现learning_rate = 0.01train_x = np.ones(shape=(rows, columns), dtype="float32")	# 这里一定要dtype一致,否则numpy与keras计算结果会有差异,我这里统一使用float32train_y = np.vstack([np.ones(shape=(int(rows/2), 1), dtype="float32"), np.zeros(shape=(int(rows/2),1), dtype="float32")])w = tf.random.normal(shape=(columns, 1), dtype=tf.float32)b = tf.zeros(shape=(1,), dtype=tf.float32)def w_init(shape, dtype=tf.float32):	return tf.convert_to_tensor(w, dtype=tf.float32)def b_init(shape, dtype=tf.float32):	return tf.convert_to_tensor(b, dtype=tf.float32)

Keras

model1 = Sequential()model1.add(Input(shape=(columns, )))model1.add(Dense(units=1, kernel_initializer=w_init, bias_initializer=b_init, activation="sigmoid"))h1 = model1.predict(train_x)model1.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate), metrics=["accuracy"])model1.fit(train_x, train_y, epochs=1, batch_size=rows)w1, b1 = model1.layers[0].weights

Tensorflow

x = tf.Variable(train_x, dtype=tf.float32)y = tf.Variable(train_y, dtype=tf.float32)with tf.GradientTape(persistent=True) as tape:	tape.watch([w, b])	y_pred = tf.sigmoid(tf.matmul(x, w) + b)	loss = -1*train_y*tf.math.log(y_pred+epsilon) - (1-train_y)*tf.math.log(1-y_pred+epsilon)dw2 = tape.gradient(target=loss, sources=w)db2 = tape.gradient(target=loss, sources=b)w2 = w - dw2*learning_rateb2 = b - db2*learning_rate

numpy

epochs = 1def sigmoid(x):	return 1/(1+np.exp(-x))w3 = wb3 = bh3 = Nonefor epoch in range(epochs):	h3 = sigmoid(np.dot(train_x, w3)+b3)	loss = -1*np.sum(train_y*np.log(h3) + (1-train_y)*np.log(1-h3))/rows	print(f"loss: {loss}")	dw3 = np.dot(train_x.T, h3-train_y)	db3 = np.dot(np.ones(shape=(1, rows), dtype="float32"), h3-train_y)	w3 = w3 - dw3*learning_rate	b3 = b3 - db3*learning_ratew3b3
epochs = 1def sigmoid(x):	return 1/(1+np.exp(-x))w4 = wb4 = bh4 = Nonefor epoch in range(epochs):	h4 = sigmoid(np.dot(train_x, w4)+b4)	loss = -1*np.sum(train_y*np.log(h4) + (1-train_y)*np.log(1-h4))/rows	print(f"loss: {loss}")	dw4 = np.dot(train_x.T, (h4-train_y-2*train_y*epsilon+epsilon)/(h4+epsilon)/(1-h4+epsilon))	dw4 = np.dot(train_x.T, -1*train_y/(h4+epsilon)+(1-train_y)/(1-h4+epsilon))	db4 = np.dot(np.ones(shape=(1, rows), dtype="float32"), h4-train_y)	w4 = w4 - dw4*learning_rate	b4 = b4 - db4*learning_ratew4b4

转载地址:http://tlugf.baihongyu.com/

你可能感兴趣的文章
seq2seq 的 keras 实现
查看>>
seq2seq 入门
查看>>
什么是 Dropout
查看>>
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>
按时间轴简述九大卷积神经网络
查看>>
详解循环神经网络(Recurrent Neural Network)
查看>>
为什么要用交叉验证
查看>>
用学习曲线 learning curve 来判别过拟合问题
查看>>
用验证曲线 validation curve 选择超参数
查看>>
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>