Tensorflow cifar10自定义网络实战

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, optimizers

def preprocess(x, y):
    #[0-255] > [-1, -1]
    x = 2 * tf.cast(x, dtype = tf.float32) / 255. - 1.
    y = tf.cast(y, dtype = tf.int32)
    return x, y
batchz = 128

#[50k, 32,32, 3], [10k,1]
(x, y), (x_test, y_test) = datasets.cifar10.load_data()
y = tf.squeeze(y)
#[10k]
y_test = tf.squeeze(y_test)
y = tf.one_hot(y, depth = 10)
y_test = tf.one_hot(y_test, depth = 10)

train_db = tf.data.Dataset.from_tensor_slices((x, y))
train_db = train_db.map(preprocess).shuffle(10000).batch(batchz)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_db = test_db.map(preprocess).shuffle(10000).batch(batchz)

class MyDense(layers.Layer):
    def __init__(self, input_dim, output_dim):
        super(MyDense, self).__init__()
        self.kernel = self.add_variable('w', [input_dim, output_dim])
    def call(self, inputs, training = None):
        out = inputs @ self.kernel
        return out
class MyModel(keras.Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc1 = MyDense(32*32*3, 256)
        self.fc2 = MyDense(256, 128)
        self.fc3 = MyDense(128, 64)
        self.fc4 = MyDense(64, 32)
        self.fc5 = MyDense(32, 10)

    def call(self, inputs, training=None):
        x = tf.reshape(inputs, [-1, 32*32*3])
        out = self.fc1(x)
        out = tf.nn.relu(out)
        out = self.fc2(out)
        out = tf.nn.relu(out)
        out = self.fc3(out)
        out = tf.nn.relu(out)
        out = self.fc4(out)
        out = tf.nn.relu(out)
        out = self.fc5(out)
        return out
network = MyModel()
network.compile(optimizer = optimizers.Adam(lr = 1e-3),
                loss = tf.losses.CategoricalCrossentropy(from_logits = True),
                metrics = ['accuracy'])
network.fit(test_db, epochs = 1, validation_data = test_db, validation_freq =1)
loss,acc = network.evaluate(test_db)
print('loss:', loss, 'accuracy:' ,acc)
network.save_weights('cifar10')
print('network_saved')
del network
network = MyModel()
network.compile(optimizer = optimizers.Adam(lr = 1e-3),
                loss = tf.losses.CategoricalCrossentropy(from_logits = True),
                metrics = ['accuracy'])
network.load_weights('cifar10')
loss,acc = network.evaluate(test_db)
print('loss:', loss, 'accuracy:' ,acc)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值