浅谈keras中loss与val_loss的关系

作者:lgy_keira 时间:2021-12-12 08:41:22 

loss函数如何接受输入值

keras封装的比较厉害,官网给的例子写的云里雾里,

在stackoverflow找到了答案

You can wrap the loss function as a inner function and pass your input tensor to it (as commonly done when passing additional arguments to the loss function).


def custom_loss_wrapper(input_tensor):
def custom_loss(y_true, y_pred):
 return K.binary_crossentropy(y_true, y_pred) + K.mean(input_tensor)
return custom_loss

input_tensor = Input(shape=(10,))
hidden = Dense(100, activation='relu')(input_tensor)
out = Dense(1, activation='sigmoid')(hidden)
model = Model(input_tensor, out)
model.compile(loss=custom_loss_wrapper(input_tensor), optimizer='adam')

You can verify that input_tensor and the loss value will change as different X is passed to the model.


X = np.random.rand(1000, 10)
y = np.random.randint(2, size=1000)
model.test_on_batch(X, y) # => 1.1974642

X *= 1000
model.test_on_batch(X, y) # => 511.15466

fit_generator

fit_generator ultimately calls train_on_batch which allows for x to be a dictionary.

Also, it could be a list, in which casex is expected to map 1:1 to the inputs defined in Model(input=[in1, …], …)


### generator
yield [inputX_1,inputX_2],y
### model
model = Model(inputs=[inputX_1,inputX_2],outputs=...)

补充知识:学习keras时对loss函数不同的选择,则model.fit里的outputs可以是one_hot向量,也可以是整形标签

我就废话不多说了,大家还是直接看代码吧~


from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
   'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# plt.figure()
# plt.imshow(train_images[0])
# plt.colorbar()
# plt.grid(False)
# plt.show()

train_images = train_images / 255.0
test_images = test_images / 255.0

# plt.figure(figsize=(10,10))
# for i in range(25):
#  plt.subplot(5,5,i+1)
#  plt.xticks([])
#  plt.yticks([])
#  plt.grid(False)
#  plt.imshow(train_images[i], cmap=plt.cm.binary)
#  plt.xlabel(class_names[train_labels[i]])
# plt.show()

model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
   loss='categorical_crossentropy',
   #loss = 'sparse_categorical_crossentropy' 则之后的label不需要变成one_hot向量,直接使用整形标签即可
   metrics=['accuracy'])
one_hot_train_labels = keras.utils.to_categorical(train_labels, num_classes=10)

model.fit(train_images, one_hot_train_labels, epochs=10)

one_hot_test_labels = keras.utils.to_categorical(test_labels, num_classes=10)
test_loss, test_acc = model.evaluate(test_images, one_hot_test_labels)

print('\nTest accuracy:', test_acc)

# predictions = model.predict(test_images)
# predictions[0]
# np.argmax(predictions[0])
# test_labels[0]

loss若为loss=‘categorical_crossentropy', 则fit中的第二个输出必须是一个one_hot类型,

而若loss为loss = ‘sparse_categorical_crossentropy' 则之后的label不需要变成one_hot向量,直接使用整形标签即可

来源:https://blog.csdn.net/u013608336/article/details/82559469

标签:keras,loss,val,loss
0
投稿

猜你喜欢

  • Python 操作文件的基本方法总结

    2021-11-29 03:18:27
  • SQL Server中TRUNCATE事务回滚操作方法

    2024-01-20 14:43:23
  • 修改vue+webpack run build的路径方法

    2024-04-28 10:54:08
  • python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)

    2024-01-13 15:19:57
  • 使用PHP获取当前url路径的函数以及服务器变量

    2023-10-31 02:31:34
  • 如何使用electron-builder及electron-updater给项目配置自动更新

    2024-04-17 10:02:04
  • 基于php权限分配的实现代码

    2023-11-14 11:10:49
  • Python编程中用close()方法关闭文件的教程

    2023-02-10 22:10:06
  • 解析:怎样掌握SQL Server中的数据查询

    2009-01-19 13:30:00
  • Python中类的初始化特殊方法

    2021-05-26 14:50:01
  • SQL语句练习实例之二——找出销售冠军

    2024-01-26 01:12:19
  • Python中栈、队列与优先级队列的实现方法

    2023-11-03 02:16:53
  • vue-router传参的4种方式超详细讲解

    2024-04-27 15:48:21
  • 简单讲解Python中的字符串与字符串的输入输出

    2021-09-14 18:57:08
  • 简单谈谈MySQL的半同步复制

    2024-01-15 01:04:50
  • Python实现的远程登录windows系统功能示例

    2022-10-09 21:26:15
  • 在Typescript中如何使用for...in详解

    2023-08-18 15:39:32
  • JavaScript函数节流和函数去抖知识点学习

    2024-06-07 15:59:54
  • mysql删除表中某一字段重复的记录

    2024-01-22 10:35:43
  • php的对象传值与引用传值代码实例讲解

    2023-11-06 08:42:37
  • asp之家 网络编程 m.aspxhome.com