parent
c9032d071f
commit
b62ce1a612
@ -1,61 +1,36 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
|
config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
|
||||||
|
sess = tf.compat.v1.Session(config=config)
|
||||||
from tensorflow import keras
|
from tensorflow import keras
|
||||||
import matplotlib.pyplot as plt
|
fashion_mnist = keras.datasets.fashion_mnist
|
||||||
|
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
|
||||||
print(tf.version.VERSION)
|
|
||||||
|
|
||||||
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
|
print(train_images.shape)
|
||||||
|
|
||||||
plt.imshow(train_images[0])
|
import tensorflow as tf
|
||||||
|
from tensorflow import keras
|
||||||
|
num_mnist = keras.datasets.mnist
|
||||||
|
(train_images, train_labels), (test_images, test_labels) = num_mnist.load_data()
|
||||||
|
|
||||||
|
train_images = train_images[:1000]
|
||||||
train_labels = train_labels[:1000]
|
train_labels = train_labels[:1000]
|
||||||
test_labels = test_labels[:1000]
|
test_images = train_images[:1000]
|
||||||
|
test_labels = train_images[:1000]
|
||||||
train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
|
|
||||||
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0
|
|
||||||
|
|
||||||
# 定义一个简单的序列模型
|
|
||||||
def create_model():
|
|
||||||
model = tf.keras.models.Sequential([
|
|
||||||
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
|
|
||||||
keras.layers.Dropout(0.2),
|
|
||||||
keras.layers.Dense(10)
|
|
||||||
])
|
|
||||||
|
|
||||||
model.compile(optimizer='adam',
|
|
||||||
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
|
|
||||||
metrics=['accuracy'])
|
|
||||||
|
|
||||||
return model
|
|
||||||
|
|
||||||
# 创建一个基本的模型实例
|
|
||||||
model = create_model()
|
|
||||||
|
|
||||||
# 显示模型的结构
|
|
||||||
model.summary()
|
|
||||||
|
|
||||||
checkpoint_path = "training_1/cp.ckpt"
|
|
||||||
checkpoint_dir = os.path.dirname(checkpoint_path)
|
|
||||||
|
|
||||||
# 创建一个保存模型权重的回调
|
model = keras.Sequential()
|
||||||
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
|
model.add(keras.layers.Conv2D(8, (3,3), activation = 'relu', input_shape = (28,28,1)))
|
||||||
save_weights_only=True,
|
model.add(keras.layers.MaxPooling2D(2,2))
|
||||||
verbose=1)
|
model.add(keras.layers.Conv2D(8, (3,3), activation = 'relu'))
|
||||||
|
model.add(keras.layers.MaxPooling2D(2,2))
|
||||||
|
|
||||||
# 使用新的回调训练模型
|
model.add(keras.layers.Flatten())
|
||||||
model.fit(train_images,
|
model.add(keras.layers.Dense(128, activation = tf.nn.relu))
|
||||||
train_labels,
|
model.add(keras.layers.Dense(36, activation = tf.nn.softmax))
|
||||||
epochs=10,
|
|
||||||
batch_size=8,
|
|
||||||
validation_data=(test_images,test_labels),
|
|
||||||
callbacks=[cp_callback]) # 通过回调训练
|
|
||||||
|
|
||||||
# 这可能会生成与保存优化程序状态相关的警告。
|
train_images_scaled = train_images/255
|
||||||
# 这些警告(以及整个笔记本中的类似警告)
|
model.compile(optimizer = 'adam', loss = tf.losses.sparse_categorical_crossentropy, metrics = ['accuracy'])
|
||||||
# 是防止过时使用,可以忽略。
|
|
||||||
|
|
||||||
results = model.evaluate(test_images, test_labels, verbose=2)
|
history = model.fit(train_images_scaled.reshape(-1, 28, 28 ,1), train_labels, epochs = 10, batch_size=8)
|
||||||
|
|
||||||
print(results)
|
#test_images_scaled = test_images/255
|
||||||
|
#results = model.evaluate(test_images_scaled.reshape(-1, 28, 28 ,1), test_labels)
|
||||||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue