简体   繁体   English

Keras - 如何正确使用 fit() 来训练 model?

[英]Keras - How to properly use fit() to train a model?

I've been familiarizing myself with Keras and going from the documentation i've put together a basic model and loaded my own folder of images to train rather than using the mnist dataset.我一直在熟悉 Keras 并从文档开始,我整理了一个基本的 model 并加载了我自己的图像文件夹来训练而不是使用 mnist 数据集。 Ive gotten to the point of setting up the model, but i'm not sure how to proceed from there in regards to calling my dataset using the fit() method and then training the model to make a prediction.我已经到了设置 model 的地步,但我不确定如何从那里开始使用 fit() 方法调用我的数据集,然后训练 model 进行预测。 Here is the code so far:这是到目前为止的代码:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers.experimental.preprocessing import CenterCrop
from tensorflow.keras.layers.experimental.preprocessing import Rescaling
from tensorflow.keras import layers

#Importing the dataset and setting the path
dataset = keras.preprocessing.image_dataset_from_directory(
    'PetImages',
    batch_size = 64,
    image_size = (200, 200)
)

dataset = keras.Input(shape = (None, None, 3))

# PreProcessing layers to better format the datset 
x = CenterCrop(height=150, width=150)(dataset)
x = Rescaling(scale=1.0 / 255)(x)

# Convolution and Pooling Layers
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)

# Global average pooling to get flat feature vectors
x = layers.GlobalAveragePooling2D()(x)

# Adding a dense classifier 
num_classes = 10
outputs = layers.Dense(num_classes, activation="softmax")(x)

# Instantiates the model once layers have been set
model = keras.Model(inputs = dataset, outputs = outputs)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# Problem: Unsure how to further call on dataset to train the model and make a prediction
model.fit()

The .fit() method is the method that actually will train your network so that it behaves in the manner that you want it to train. .fit()方法是实际训练您的网络的方法,以便它以您希望它训练的方式运行。 Models require data in order to be trained.模型需要数据才能进行训练。 I would take a look at their documentation or some of their examples to see how to go forward with your model as a good starting place.我会查看他们的 文档或他们的一些示例,以了解如何将 go 与您的 model 作为一个良好的起点。

Depending on the version of tensorflow.keras that you are using, .fit can either take two positional arguments x , and y or it can take a generator object, which is something that acts like a continuously active function. Depending on the version of tensorflow.keras that you are using, .fit can either take two positional arguments x , and y or it can take a generator object, which is something that acts like a continuously active function. You might also want to set a batch_size , which is essentially how many samples to evaluate at once.您可能还想设置一个batch_size ,它本质上是一次评估多少个样本。 Again, the documentation will have a lot more information on what sort of parameters it can take.同样,文档将包含更多关于它可以采用哪种参数的信息。

In your case, it seems like you've gotten some good input images in the variable dataset (which you promptly overwrite), but you have no labels .在您的情况下,您似乎在变量dataset获得了一些好的输入图像(您会立即覆盖),但您没有标签 Labels define what the expected output is for your training images you input.标签定义了您输入的训练图像的预期 output 是什么。 The first step you'll need is a set of labels, and then, below are some adjustments you can make to your code to make it run:您需要的第一步是一组标签,然后,您可以对代码进行一些调整以使其运行:

# Add line below
labels = # ... load labels from someplace, like how you loaded the images

# Change this
dataset = keras.Input(shape = (None, None, 3))
# to this
input_layer = layers.Input(shape=(None, None, 3))

# Change this
model = keras.Model(inputs = dataset, outputs = outputs)
# to this
model = keras.models.Model(inputs = input_layer, outputs = outputs)

# and finally you can fit your model using
model.fit(dataset, labels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确使用keras fit_generator - How to use keras fit_generator properly 如何在keras中使用model.fit_generator - How to use model.fit_generator in keras Keras train_on_batch()不训练模型vs fit() - Keras train_on_batch() does not train the model vs fit() Keras:我可以使用model.predict而不使用model.predict_generator来预测是否使用model.fit_generator训练模型 - Keras: can I use model.predict but not model.predict_generator to predict if I train the model with model.fit_generator 如何使用单个数据集在 tensorflow keras 中训练多个输入 model - how to use a single dataset to train multiple input model in tensorflow keras keras:如何在 model.train_on_batch() 中使用学习率衰减 - keras: how to use learning rate decay with model.train_on_batch() 如何使用OpenImages数据集训练Keras中的二进制model - How to use OpenImages dataset to train binary model in Keras Keras fit_generator() 没有正确训练 - Keras fit_generator() doesn't train properly 如果 Keras Model.fit() 给出了输入序列数组的列表,则输入应如何关联/映射到 label y > - How input should relate/map to label y if Keras Model.fit() is given a list of input train arrays> 从 Sklearn 获得训练和测试集后,如何将 model 放入 Keras - How do I fit a model in Keras after getting Train and test set from Sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM