CRNN add digits

# for reproducibility
np.random.seed(2016)
K.set_image_dim_ordering('tf')
# define some run parameters
batch_size = 32
nb_epochs = 100
examplesPer = 60000
maxToAdd = 8
hidden_units = 200
size = 28
# cutoff          = 1000

# the data, shuffled and split between train and test sets
(X_train_raw, y_train_temp), (X_test_raw, y_test_temp) = mnist.load_data()

# ignore "cutoff" section in full run
# X_train_raw     = X_train_raw[:cutoff]
# X_test_raw      = X_test_raw[:cutoff]
# y_train_temp    = y_train_temp[:cutoff]
# y_test_temp     = y_test_temp[:cutoff]

# basic image processing
X_train_raw = X_train_raw.astype('float32')
X_test_raw = X_test_raw.astype('float32')
X_train_raw /= 255
X_test_raw /= 255

print('X_train_raw shape:', X_train_raw.shape)
print(X_train_raw.shape[0], 'train samples')
print(X_test_raw.shape[0], 'test samples')
print("Building model")

# define our time-distributed setup
model = Sequential()
model.add(TimeDistributed(Conv2D(8, (4, 4), padding='valid'), input_shape=(maxToAdd, size, size, 1)))
model.add(Activation('relu'))
model.add(TimeDistributed(Conv2D(16, (3, 3), padding='valid')))
# model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),border_mode='valid')))
# model.add(Activation('relu'))
# model.add(TimeDistributed(Convolution2D(8, 3, 3, border_mode='valid')))
model.add(Activation('relu'))
# model.add(Reshape((maxToAdd,np.prod(model.output_shape[-3:])))) #this line updated to work with keras 1.0.2
model.add(TimeDistributed(Flatten()))
model.add(Activation('relu'))
model.add(GRU(units=100, return_sequences=True))
model.add(GRU(units=50, return_sequences=False))
model.add(Dropout(.2))
model.add(Dense(1))

'''
# define our time-distributed setup
model = Sequential()
model.add(TimeDistributed(Convolution2D(8, 4, strides=4, padding='valid', activation='relu'),
                          input_shape=(SEQ_LENGTH, height, width, depth)))
model.add(TimeDistributed(Convolution2D(16, 3, strides=3, padding='valid', activation='relu')))
model.add(TimeDistributed(Flatten()))
model.add(GRU(50, return_sequences=True, dropout=.3))
model.add(TimeDistributed(Dense(10, activation='softmax')))
'''
rmsprop = RMSprop()model.compile(loss='mean_squared_error', optimizer=rmsprop)import osif os.path.exists('xxx.h5'): model = load_model('xxx.h5')# run epochs of sampling data then trainingfor ep in range(0, nb_epochs): X_train = [] y_train = [] X_test = [] y_test = [] X_train = np.zeros((examplesPer, maxToAdd, size, size, 1)) for i in range(0, examplesPer): # initialize a training example of max_num_time_steps,im_size,im_size output = np.zeros((maxToAdd, size, size, 1)) # decide how many MNIST images to put in that tensor numToAdd = int(np.ceil(np.random.rand() * maxToAdd)) # sample that many images indices = np.random.choice(X_train_raw.shape[0], size=numToAdd) example = X_train_raw[indices] # sum up the outputs for new output exampleY = y_train_temp[indices] output[0:numToAdd, :, :, 0] = example X_train[i, :, :, :, :] = output y_train.append(np.sum(exampleY)) y_train = np.array(y_train) if ep == 0: print("X_train shape: ", X_train.shape) print("y_train shape: ", y_train.shape) model.fit(X_train, y_train, batch_size=batch_size, epochs=1, verbose=1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值