今天实验了如下代码:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
def preprocess(x, y):
x = 2*tf.cast(x,dtype=tf.float32)/255.0-1.0
y = tf.cast(y,dtype=tf.int32)
return x,y
(x,y), (x_test,y_test) = datasets.cifar10.load_data()
y = tf.squeeze(y)
y_test = tf.squeeze(y_test)
y = tf.one_hot(y, depth = 10)
y_test = tf.one_hot(y_test, depth = 10)
print('dataset', x.shape, y.shape, x_test.shape, y_test.shape)
train_db = tf.data.Dataset.from_tensor_slices((x,y))
train_db = train_db.map(preprocess)#.shuffle(10000).batch(128)
test_db = tf.data.Dataset.from_tensor_slices((x_test,y_test))
test_db = test_db.map(preprocess)#.batch(128)
class MyDense(layers.Layer):
def __init__(self, inp_dim, out_dim):
super(MyDense, self).__init__()
self.kernel = self.add_weight('w', [inp_dim, out_dim])
def call(self, inputs, training = None):
a = inputs @ self.kernel
return a
class MyNetwork(keras.Model):
def __init__(self):
super(MyNetwork, 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):
a0 = tf.reshape(inputs, [-1,32*32*3])
a1 = self.fc1(a0)
a1 = tf.nn.relu(a1)
a2 = self.fc2(a1)
a2 = tf.nn.relu(a2)
a3 = self.fc3(a2)
a3 = tf.nn.relu(a3)
a4 = self.fc4(a3)
a4 = tf.nn.relu(a4)
a5 = self.fc5(a4)
return a5
network = MyNetwork()
network.build(input_shape = [None, 32*32*3])
network.compile(optimizer=optimizers.Adam(lr=1e-3),
loss=tf.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accurancy'])
network.fit(train_db, epochs=15, validation_data=test_db, validation_freq=1)
报错:You must feed a value for placeholder tensor ‘flatten_input’ with dtype float and shape [?,28,28]。先不说别的,这个28是怎么来的。。。百思不得其解。