问题
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**, ...)
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.
Thank you in advance!
ADDENDUM1:
For instance, assume the following code piece
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)
In this example, when I later run the fit and/or evaluate functions from Keras, how should I set the y parameter?
ADDENDUM 2:
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)
回答1:
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)))
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.
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.
来源:https://stackoverflow.com/questions/51203005/y-parameter-in-keras-fit-and-evaluate-functions