AI开发框架

Keras

Keras是一个简洁高效的深度学习API,支持多框架,适合快速构建和部署模型。

标签:

Keras 是什么
Keras是一个为人类设计的深度学习API,而非机器。它专注于调试速度、代码优雅与简洁、可维护性和可部署性。使用Keras时,代码库更小、更易读、更易于迭代。Keras采用多后端方法,支持JAX、TensorFlow和PyTorch,使用户能够自由选择框架并利用各生态系统的优势。

Keras 使用示例
以下是一个简单的Keras模型构建和训练示例:

  1. 构建模型
import keras  
from keras import layers  

inputs = keras.Input(shape=(32, 32, 3))  
x = layers.Conv2D(32, 3, activation="relu")(inputs)  
x = layers.Conv2D(64, 3, activation="relu")(x)  
x = layers.GlobalAveragePooling2D()(x)  
outputs = layers.Dense(10, activation="softmax")(x)  
model = keras.Model(inputs, outputs, name="mini_cnn")  
  1. 训练模型
model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])  
model.fit(dataset, epochs=10)  
  1. 使用KerasHub预训练模型
causal_lm = keras_hub.models.CausalLM.from_preset("gemma2_instruct_2b_en", dtype="float16")  
prompt = "Write python code to print the first 100 primes."  
text_output = causal_lm.generate(prompt, max_length=512)  

相关导航

暂无评论

暂无评论...