3

I am trying to write a GAN in keras but I am getting this assertion error while running it. After searching I found out the most likely cause of the problem is an old version theano. I updated theano to the latest github development version 0.9.0beta1 but still I am getting the same error.

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD

print "Setting up decoder"
D = Sequential()
D.add(Dense(100, input_dim=100, activation='relu'))
D.add(Dropout(0.5))
D.add(Dense(50, activation='relu'))
D.add(Dropout(0.5))
D.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.01, momentum=0.1)
D.compile(loss='binary_crossentropy', optimizer=sgd)

print "Setting up generator"
G = Sequential()
G.add(Dense(1, input_dim=1, activation='relu'))
G.add(Dropout(0.5))
G.add(Dense(50, activation='relu'))
G.add(Dropout(0.5))
G.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.01, momentum=0.1)
G.compile(loss='binary_crossentropy', optimizer=sgd)

print "Setting up combined net"
gen_dec = Sequential()
gen_dec.add(G)
D.trainable=False
gen_dec.add(D)
gen_dec.compile(loss='binary_crossentropy', optimizer=sgd)
gen_dec.summary()

The problem happens in this section gen_dec.add(D)

assert input_shape[-1] and input_shape[-1] == self.input_dim
AssertionError
4

1 回答 1

1

I think it's a typo in your code... Please change the last layer of the Generator from:

G.add(Dense(1, activation='sigmoid'))

to:

G.add(Dense(100, activation='sigmoid'))

I guess you didn't want your generator to produce only 1 pixel since your discriminator takes 100 inputs.

The error came from the fact that your first model outputs a tensor with shape (batch_size, 1) and your second model input takes a input shape of (batch_size, 100). Hence the assert error.

It's compiling on my laptop now.

于 2017-02-20T11:20:48.830 回答