tensorflow2.0学习记录-三种建模方式Three modeling methods

这篇博客介绍了TensorFlow2.0中的三种建模方式:Sequential模型,适合初学者;函数式模型,适用于更灵活的模型构建;以及Subclassing模型,提供完全自定义的灵活性。作者建议初学者从函数式模型开始,进阶后再尝试Subclassing模型。关键概念包括模型训练的epochs和batch_size,以及模型的验证数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇blog介绍了tensorflow2.0的三种建模方式。
三种建模
初学者建议前两种,学到后期强烈建议后一种,可以高度自定义。

顺序模型Sequential model

tf.keras.Sequential

就像搭积木一样,一层一层往上搭建。
第一种搭积木方式:

from tensorflow.keras import layers
import tensorflow as tf
model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu'))#第一层
model.add(layers.Dense(64, activation='relu'))#第二层
model.add(layers.Dense(10))#第三层
#。。。未完待续

还有另外一种

model = tf.keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(32,)),#第一层
layers.Dense(64, activation='relu'),#第二层
layers.Dense(10)#第三层
    #。。。。。全连接层
])

搭建好之后开始训练模型,定义损失函数,定义优化器,定义评估函数(可以不定义)。

model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
import numpy as np

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))

model.fit(data, labels, epochs=10, batch_size=32)#开始训练model.fit()
Train on 1000 samples
Epoch 1/10
WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x000002CA715BB168> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: No module named 'tensorflow_core.estimator'
WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x000002CA715BB168> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值