Keras中的y参数拟合和评估函数 [英] y parameter in Keras fit and evaluate functions

查看:117
本文介绍了Keras中的y参数拟合和评估函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Keras文档说,fit和valuate函数中的y参数可以设置为None,这实际上是默认值. (我在下面缩短了功能定义,以节省一些空间.)

The Keras documentation says that the y parameter in fit and evaluate functions can be set to None, which actually is the default. (I shorten the function definitions below to save some space.)

fit(self, x=None, **y=None**,  ...)

evaluate(self, x=None, **y=None**, ...)

文档将值 的动机如下: 如果从框架本机张量(例如TensorFlow数据张量)馈送,则y可以为None(默认)."

And the documentation motivates the value None as following: "y can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors)."

这并不能告诉我太多.谁能解释这到底意味着什么?如果可以举一个简短的例子,将不胜感激.

This does not really tell me that much. Could anyone explain what this really means? If it would be possible to give a short example, it would be highly appreciated.

提前谢谢!

ADDENDUM1:

例如,假设以下代码段

model = ResNet50(weights='imagenet')
x = model.get_layer('flatten_1').output # layer 'flatten_1' is the last layer of the "model"
model_out = Dense(128, activation='relu',  name='model_out')(x)
model_out = Lambda(lambda  x: K.l2_normalize(x,axis=-1))(model_out)

new_model = Model(inputs=model.input, outputs=model_out)

anchor_input = Input(shape=(224, 224, 3), name='anchor_input')
pos_input = Input(shape=(224, 224, 3), name='pos_input')
neg_input = Input(shape=(224, 224, 3), name='neg_input')

encoding_anchor   = new_model(anchor_input)
encoding_pos      = new_model(pos_input)
encoding_neg      = new_model(neg_input)

loss = Lambda(triplet_loss)([encoding_anchor, encoding_pos, encoding_neg])

siamese_network = Model(inputs  = [anchor_input, pos_input, neg_input], 
                        outputs = loss)

siamese_network.compile(optimizer=Adam(lr=.00003), loss=identity_loss)

在此示例中,当我稍后运行Keras的fit和/或评估函数时,应如何设置 y 参数?

In this example, when I later run the fit and/or evaluate functions from Keras, how should I set the y parameter?

附录2:

这是上面的代码中提到的Triplet_loss函数:

And here is the triplet_loss function mentioned in the above code:

def triplet_loss(inputs):
    anchor, positive, negative = inputs
    positive_distance = K.square(anchor - positive)
    negative_distance = K.square(anchor - negative)
    positive_distance = K.sqrt(K.sum(positive_distance, axis=-1, keepdims=True))
    negative_distance = K.sqrt(K.sum(negative_distance, axis=-1, keepdims=True))
    loss = K.maximum(0.0, 2 + loss)
    return K.mean(loss)

推荐答案

这是一个很好的问题.即使不经常使用,Keras允许提供框架本机张量,而不是通过占位符提供.考虑以下示例:

That's a good question. Even though this is not frequently used, Keras allows to feed framework-native tensors instead of feeding via placeholders. Consider the following example:

from keras.models import Model
from keras.layers import Dense, Input
import tensorflow as tf

# TensorFlow native tensors
a = tf.random_uniform(shape=(32, 1,), maxval=1)
b = 2*a

# Keras model
x = Input(shape=(1,), tensor=a)
h = Dense(1)(x)
model = Model(x, h)
model.compile('sgd', 'mse', target_tensors=[b])

# Train + evaluate
model.fit(steps_per_epoch=1000)
print('MSE: {}'.format(model.evaluate(steps=10)))

在这里,我们通过 keras.layers中的参数tensor指定模型的输入.输入.在这种情况下,Keras没有定义占位符(通常会通过 model .fit ).相反,TensorFlow张量a直接连接到x.同样,可以通过 model.compile target_tensors定义目标.

Here, we specify the input of our model via the argument tensor from keras.layers.Input. In this case Keras does not define a placeholder (which you would usually be feeding through the argument x from model.fit). Instead, the TensorFlow tensor a is directly connected to x. Similarly, one can define the target via target_tensors of model.compile.

从框架本机张量进给时,应将model.fit中的参数steps_per_epoch设置为构成一个纪元的批处理数量,而将steps中的参数steps设置为

When you feed from framework-native tensors, the argument steps_per_epoch from model.fit should be set to the number of batches that constitute an epoch, and the argument steps from model.evaluate is then the number of batches used for evaluating the model.

这篇关于Keras中的y参数拟合和评估函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆