keras使用详解

1 概述

  • keras官方文档 有全部相关API使用
  • 写了后发现,keras API文档很多用法都很详细,后面的内容别看了

2 Model层

2.1 Model

Model函数的使用:

from keras input Model
model = Model()
# 包含以下三个参数
# Arguments:
#    inputs: The input(s) of the model: a keras.Input object or list of keras.Input objects.
#    outputs: The output(s) of the model. See Functional API example below.
#    name: String, the name of the model.

# 使用
from keras.models import Input
from keras.layers import Dense
inputs = Input(shape=(3,))
x = Dense(4, activation="relu")(inputs)
outputs = Dense(5, activation="softmax")(x)
model = Model(inputs=inputs, outputs=outputs)

Model函数的方法:

  1. summary 方法:
Model.summary(line_length=None, positions=None, print_fn=None)
# Arguments
#    line_length: Total length of printed lines (e.g. set this to adapt the display to different terminal window sizes).
#    positions: Relative or absolute positions of log elements in each line. If not provided, defaults to [.33, .55, .67, 1.].
#    print_fn: Print function to use. Defaults to print. It will be called on each line of the summary. You can set it to a custom function in order to capture the string summary.

  1. get_layer 方法:
Model.get_layer(name=None, index=None)
# Arguments
#    name: String, name of layer.
#    index: Integer, index of layer.

2.2 Sequential

Sequential 函数的使用:

# 使用
from keras import Sequential
from keras.models import Input
from keras.layers import Dense

model = Sequential()
inputs = Input(shape=(3,))
model.add(inputs)
model.add(Dense(4, activation="relu"))
model.compile(optimizer='sgd', loss='mse')
model.fit(x, y, batch_size=32, epochs=10)

Sequential 函数的方法:

  1. add 方法:
Sequential.add(layer)Sequential.add(layer)
# Arguments
#    layer: layer instance.
  1. pop 方法:
## Removes the last layer in the model.
Sequential.pop()

2.3 Model 训练

  1. compile 方法:
from keras import Model
Model.compile(
    optimizer="rmsprop",
    loss=None,
    metrics=None,
    loss_weights=None,
    weighted_metrics=None,
    run_eagerly=None,
    steps_per_execution=None,
    **kwargs
)
  1. fit 方法
Model.fit(
    x=None,
    y=None,
    batch_size=None,
    epochs=1,
    verbose=1,
    callbacks=None,
    validation_split=0.0,
    validation_data=None,
    shuffle=True,
    class_weight=None,
    sample_weight=None,
    initial_epoch=0,
    steps_per_epoch=None,
    validation_steps=None,
    validation_batch_size=None,
    validation_freq=1,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
)
  1. evaluate 方法
Model.evaluate(
    x=None,
    y=None,
    batch_size=None,
    verbose=1,
    sample_weight=None,
    steps=None,
    callbacks=None,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
    return_dict=False,
)

  1. predict 方法
Model.predict(
    x,
    batch_size=None,
    verbose=0,
    steps=None,
    callbacks=None,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
)

猜你喜欢

转载自blog.csdn.net/weixin_41466575/article/details/113529719