我尝试适合这个模型:
from keras.layers.convolutional import Conv2D, MaxPooling2D, AveragePooling2D
from keras import backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Activation, Reshape,Flatten
from keras.optimizers import *
from keras.layers.advanced_activations import LeakyReLU
from keras.layers import Dense,Reshape,Lambda,Dropout,Activation
import numpy as np
from keras.layers.embeddings import Embedding
from keras.datasets import mnist,cifar10
from keras.utils import np_utils
import matplotlib.pyplot as plt
from keras.callbacks import ModelCheckpoint
img_rows, img_cols = 32, 32
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# normalize inputs from 0-255 to 0-1
X_train = X_train / 128.0
X_test = X_test / 128.0
print X_train.shape
print X_test.shape
y_train=np_utils.to_categorical(y_train)
y_test=np_utils.to_categorical(y_test)
Dict_size=64
EmbedSz=10
img_sz=32
channels=3
dopt = Adam(lr=1e-4)
dropout_rate = 0.5
filepath="weights.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor= 'val_acc' , verbose=1, save_best_only=True,mode= 'max')
callbacks_list = [checkpoint]
d_input=Input(shape=(img_sz,img_sz,channels))
H=Conv2D(Dict_size,(3, 3) , activation='relu', border_mode='same')(d_input)
H=Lambda(lambda x:K.argmax(x, axis=3),output_shape=lambda s: (None,img_sz,img_sz))(H)
H=Reshape((1,img_sz*img_sz))(H)
H=Embedding(Dict_size, EmbedSz, input_length=img_sz*img_sz)(H)
H=Reshape((img_sz,img_sz,EmbedSz))(H)
H = Conv2D(64, (3, 3), subsample=(2, 2), border_mode = 'same', activation='relu')(H)
H =Activation('relu')(H)
H = Dropout(dropout_rate)(H)
H = Conv2D(256,( 3,3), subsample=(2, 2), border_mode = 'same', activation='relu')(H)
H = Activation('relu')(H)
H = Dropout(dropout_rate)(H)
H = Conv2D(256,( 3,3), subsample=(2, 2), border_mode = 'same', activation='relu')(H)
H = Activation('relu')(H)
H = Dropout(dropout_rate)(H)
H = Flatten()(H)
H = Dense(256)(H)
H = Activation('relu')(H)
H = Dropout(dropout_rate)(H)
d_V = Dense(10,activation='softmax')(H)
discriminator = Model(d_input,d_V)
discriminator.compile(loss='categorical_crossentropy', optimizer=dopt,metrics=['accuracy'])
discriminator.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=128)
ar=np.random.rand(15,img_sz,img_sz,channels)
print ('input',ar.shape)
pr=discriminator.predict(ar)
print('output',pr.shape)
但我得到了:
File "/home/adel/keras-resnet-master/tryarg.py", line 82, in <module>
discriminator.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=128)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1476, in fit
self._make_train_function()
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1008, in _make_train_function
self.total_loss)
File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 398, in get_updates
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 883, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 651, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 716, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 176, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 165, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 360, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.
我检查了所有张量的大小是没关系的,这个问题只有在我调用适合函数时才会出现!甚至预测工作只适合不起作用!
ValueError:不支持任何值。