pip install tensorflow
pip install keras
import keras
print(keras.__version__)
from tensorflow.keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, )
y_test = to_categorical(y_test, )
from tensorflow.keras.applications import ResNet50
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(, , ))
weights='imagenet'
表示加载在 ImageNet 数据集上预训练的权重,include_top=False
表示不包含顶层的全连接层,input_shape=(32, 32, 3)
指定输入图像的尺寸为 32x32 像素,3 通道。for layer in base_model.layers:
layer.trainable = False
from tensorflow.keras.layers import GlobalAveragePooling2D
x = GlobalAveragePooling2D()(base_model.output)
from tensorflow.keras.layers import Dense
x = Dense(, activation='relu')(x)
predictions = Dense(, activation='softmax')(x)
from tensorflow.keras.models import Model
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
ImageDataGenerator
类来实现实时数据增强。from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True
)
datagen.fit(x_train)
history = model.fit(
datagen.flow(x_train, y_train, batch_size=),
epochs=,
validation_data=(x_test, y_test)
)
ModelCheckpoint
可以保存性能最佳的模型,EarlyStopping
可以在验证集损失不再下降时提前停止训练。from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
checkpoint = ModelCheckpoint(
'best_model.h5',
monitor='val_accuracy',
save_best_only=True,
mode='max'
)
early_stopping = EarlyStopping(
monitor='val_loss',
patience=,
mode='min'
)
history = model.fit(
datagen.flow(x_train, y_train, batch_size=),
epochs=,
validation_data=(x_test, y_test),
callbacks=[checkpoint, early_stopping]
)
loss, accuracy = model.evaluate(x_test, y_test)
print(f'Test loss: {loss:.4f}')
print(f'Test accuracy: {accuracy:.4f}')
import numpy as np
# 假设我们有一张新的图像,尺寸为32x32x3
new_image = np.expand_dims(new_image, axis=)
new_image = new_image.astype('float32') / 255.0
predictions = model.predict(new_image)
predicted_class = np.argmax(predictions)
for layer in base_model.layers[-:]:
layer.trainable = True
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(
datagen.flow(x_train, y_train, batch_size=),
epochs=,
validation_data=(x_test, y_test),
callbacks=[checkpoint, early_stopping]
)
model.save('image_classification_model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('image_classification_model.tflite', 'wb') as f:
f.write(tflite_model)
现在做自媒体、写文案的朋友,几乎天天都得跟 AI 打交道。但最头疼的事儿来了 —— 明明是自己琢磨半天写的指令,生成的内容却总在 AI 检测里栽跟头,通过率低到让人想摔键盘。这到底是为啥?手机端和电脑
🔍精准定位:打造高价值粉丝群体 做公众号变现,第一步得搞清楚自己到底要吸引哪类人。很多人刚开始做号时啥内容都发,今天发情感故事,明天发职场干货,结果粉丝啥类型都有,看着数量挺多,变现时才发现难搞。为
做情感故事公众号的都知道,单靠公众号本身的流量池很难突破增长瓶颈。想让故事被更多人看到,多平台分发是必经之路。但不是把公众号文章直接复制粘贴到其他平台就行 —— 每个平台的用户习惯、内容偏好天差地别,
公众号运营到 500 粉,就像游戏里闯过第一关 BOSS。别小看这 500 个粉丝,它可是从 “为爱发电” 到 “有点收入” 的分水岭。很多人卡在 300 粉就放弃了,其实再往前一步,就能摸到变现的门
🕵️♂️ 为什么说朱雀 AI 是当前最好用的 AI 文章检测工具? 用过不下 20 款 AI 检测工具的人都知道,现在市面上的产品大多逃不过两个问题:要么收费贵得离谱,动辄几百块的月费;要么检测结
写 prompt 这事儿,很多人觉得就是把问题敲进去就行。但你试试用同样的词问 ChatGPT 和 Claude,出来的东西可能天差地别。真正的高级 prompt 写作,藏着一套反直觉的底层逻辑 ——
写 prompt 就像给 AI 下达指令,指令越清晰准确,AI 的输出就越靠谱。要是 prompt 写得稀里糊涂,AI 很可能就会 “胡说八道”。那怎么才能让 AI 不胡说呢?这里有一些 2025 年
🔍 实测多款热门工具 | 找到你的演示文稿神器 AI 生成 PPT 模板的质量究竟如何?这是很多职场人、学生和创业者关心的问题。我花了两周时间实测了市面上 12 款主流工具,从内容逻辑到设计美感,从