def Encoder(img_size, in_channel, conv_channel, filter_size, latent_dim, dense_size, bn): inner_conv_channel = conv_channel // 2 if img_size % 4 != 0: print("WARNING: image size mod 4 != 0, may produce bug.") # total input number of the input of the last conv layer, new image size = old / 2 / 2 flatten_img_size = inner_conv_channel * img_size / 4 * img_size / 4 # explain: first two layer's padding = 2, because we set W/S = W/S + floor((-F+2P)/S+1), S=2,F=5,so P=2 if VERBOSE: print(img_size, in_channel, conv_channel, filter_size, latent_dim, bn) model = nn.Sequential( layers.ConvLayer(in_channel, conv_channel, filter_size, stride=2, padding=2, bn=bn), layers.ConvLayer(conv_channel, inner_conv_channel, filter_size, stride=2, padding=2, bn=bn), layers.ConvLayer(inner_conv_channel, inner_conv_channel, filter_size, stride=1, padding=2, bn=bn), layers.Flatten(), layers.Dense(flatten_img_size, dense_size), layers.Dense(dense_size, latent_dim)) model = model.to(device=device, dtype=dtype) model = torch.nn.DataParallel(model, device_ids=GPU_IDs) return model
def create_network(available_actions_count): # Create the input variables s1 = T.tensor4("State") a = T.vector("Action", dtype="int32") q2 = T.vector("Q2") r = T.vector("Reward") isterminal = T.vector("IsTerminal", dtype="int8") # Create the input layer of the network. inputLayer = s1 new_w = resolution[0] new_h = resolution[1] # Add 2 convolutional layers with ReLu activation # filter_shape = [num_filters, num_input_feature_maps, filter_height, filter_width] input_shape_1 = [batch_size, 1, resolution[0], resolution[1]] filter_shape_1 = [8, 1, 6, 6] layer1 = layers.ConvLayer(input=inputLayer, filter_shape=filter_shape_1, input_shape=input_shape_1, pool_size=None) new_w = (new_w - filter_shape_1[2] + 1)/1 # No pooling new_h = (new_h - filter_shape_1[3] + 1)/1 # No pooling input_shape_2 = [batch_size, 8, new_w, new_h] filter_shape_2 = [8, 8, 3, 3] layer2 = layers.ConvLayer(input=layer1.out, filter_shape=filter_shape_2, input_shape=input_shape_2, pool_size=None) # Add a single fully-connected layer. new_w = (new_w - filter_shape_2[2] + 1)/1 # No pooling new_h = (new_h - filter_shape_2[3] + 1)/1 # No pooling layer3 = layers.FCLayer(input=layer2.out.flatten(2), fan_in=filter_shape_2[0]*new_w*new_h, num_hidden=128) # Add the output layer (also fully-connected). # (no nonlinearity as it is for approximating an arbitrary real function) layer4 = layers.FCLayer(input=layer3.out, fan_in=128, num_hidden=available_actions_count, activation=None) layer4_out = layer4.out q = layer4_out # target differs from q only for the selected action. The following means: # target_Q(s,a) = r + gamma * max Q(s2,_) if isterminal else r target_q = T.set_subtensor(q[T.arange(q.shape[0]), a], r + discount_factor * (1 - isterminal) * q2) loss = squared_error(q, target_q).mean() # Update the parameters according to the computed gradient using RMSProp. params = layer4.params + layer3.params + layer2.params + layer1.params updates = rmsprop(loss, params, learning_rate) # Compile the theano functions print("Compiling the network ...") function_learn = theano.function([s1, q2, a, r, isterminal], loss, updates=updates, name="learn_fn") function_get_q_values = theano.function([s1], q, name="eval_fn") function_get_best_action = theano.function([s1], T.argmax(q, axis=1), name="test_fn") print("Network compiled.") def simple_get_best_action(state): state = np.expand_dims(state, axis=0) state = np.expand_dims(state, axis=0) state = np.repeat(state, batch_size, axis=0) return function_get_best_action(state) # Returns Theano objects for the net and functions. return params, function_learn, function_get_q_values, simple_get_best_action
def Decoder(s_dim, z_dim, img_size, img_channel, conv_channel, filter_size, dense_size, bn): # TODO # essentially the mirror version of Encoder inner_conv_channel = conv_channel // 2 back_img_size = img_size // 4 flatten_img_size = inner_conv_channel * back_img_size * back_img_size input_dim = s_dim + z_dim pad = int( np.floor(filter_size / 2)) # chose pad this way to fullfill floor((-F+2P)/1+1)==0 model = nn.Sequential( layers.Dense(input_dim, dense_size), layers.Dense(dense_size, inner_conv_channel * back_img_size * back_img_size), layers.Reshape((-1, inner_conv_channel, back_img_size, back_img_size)), layers.ConvLayer(inner_conv_channel, inner_conv_channel, filter_size, stride=1, padding=pad, bn=bn, upsampling=True), layers.ConvLayer(inner_conv_channel, conv_channel, filter_size, stride=1, padding=pad, bn=bn, upsampling=True), layers.ConvLayer(conv_channel, img_channel, filter_size, stride=1, padding=pad, bn=bn, upsampling=False), ) model = model.to(device=device, dtype=dtype) model = torch.nn.DataParallel(model, device_ids=GPU_IDs) return model
def CNN(): net = n.NeuralNetwork([ l.InputLayer(height=28, width=28), l.ConvLayer(10, kernel=3, init_val=o.randSc, activator=o.sigmoid), l.MaxLayer(pool=2), l.FCLayer(height=10, init_val=o.randSc, activator=o.softmax) ], o.crossentropy) optimizer = o.SGD(0.1) num_epochs = 2 batch_size = 50 num_class = 2 return net, optimizer, num_epochs, batch_size, num_class
def __init__(self, in_channels=3): super(Encoder, self).__init__() layers = [nn.InstanceNorm2d(in_channels, affine=True)] layers.append(torch.nn.ReflectionPad2d(15)) layer_spec = [[3, 32, 3, 1], [32, 32, 3, 2], [32, 64, 3, 2], [64, 128, 3, 2], [128, 256, 3, 2]] pad = True for spec in layer_spec: in_channels, out_channels, kernel_size, stride = spec layers.append( L.ConvLayer(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, pad='VALID')) self.layers = nn.Sequential(*layers)
def Loaded_nn(name='save(cnn(3_5_2)(0_1_2_8))'): batch_of_weights = load_weights(name) ccn_pack, fcl_pack = batch_of_weights cnn_b, cnn_w = ccn_pack fcl_b, fcl_w = fcl_pack cnn_w = map(float, np.delete(cnn_w.split('\n'), -1)) cnn_b = map(float, np.delete(cnn_b.split('\n'), -1)) fcl_w = map(float, np.delete(fcl_w.split(', '), -1)) fcl_b = map(float, np.delete(fcl_b.split(', '), -1)) ''' fcl_w = map(float,np.delete(fcl_w.split('\n'),-1)) fcl_b = map(float,np.delete(fcl_b.split('\n'),-1)) ''' weights = [cnn_w, cnn_b, fcl_w, fcl_b] con_layer = l.ConvLayer(3, kernel=5, init_val=o.randSc, activator=o.sigmoid) fc_layer = l.FCLayer(height=10, init_val=o.randSc, activator=o.softmax) net = NeuralNetwork([ l.InputLayer(height=28, width=28), con_layer, l.MaxLayer(pool=2), fc_layer ], o.crossentropy) con_layer.w = np.reshape(weights[0], (np.shape(con_layer.w))) con_layer.b = np.reshape(weights[1], (np.shape(con_layer.b))) fc_layer.w = np.reshape(weights[2], (np.shape(fc_layer.w))) fc_layer.b = np.reshape(weights[3], (np.shape(fc_layer.b))) return net
def CPRS(network_list): #print(train_data[0].shape) #Xdim = ( Xnch, Xrow, Xcol ) ds1 = (2, 2) #st1 = ( 2, 2 ) input = {} weight_dim = {} output = {} output_num = {} #入力パラメータ #input_dim = train_data[0].shape input_dim = (params_dict["InputChannel"], params_dict["InputWidth"], params_dict["InputHeight"]) #weight_dim[0] = ( 16, input_dim[0], 5, 5 ) #output_num[0] = 100 #output_num[1] = 10 reshape_flag = True #affine_cnt = 0 layers = [] gamma = {} beta = {} #for i in range( len( network_list ) ): for i in range(params_dict['LayerNum']): if network_list[i] == "BatchNorm": input[i] = output[i - 1] output[i] = input[i] gamma[i] = theano.shared( np.ones(input[i], dtype=theano.config.floatX)) beta[i] = theano.shared( np.zeros(input[i], dtype=theano.config.floatX)) layers.append(convnet.BatchNormLayer(input[i], gamma[i], beta[i])) if network_list[i] == "Convolution": input[i] = input_dim if i == 0 else output[i - 1] weight_dim[i] = (params_dict['Channel' + str(i + 1)], input[i][0], params_dict['Width' + str(i + 1)], params_dict['Height' + str(i + 1)]) output[i] = (weight_dim[i][0], input[i][1] - weight_dim[i][2] + 1, input[i][2] - weight_dim[i][3] + 1) layers.append(convnet.ConvLayer(input[i], weight_dim[i])) #elif network_list[i] == "Pool": elif network_list[i] == "Pooling": input[i] = output[i - 1] if input[i][1] % 2 == 0: #input[i][1] = input[i][1] + 1 #input[i][2] = input[i][2] + 1 #print(input[i][1]) #print(input[i][2]) output[i] = (input[i][0], input[i][1] / 2, input[i][2] / 2) else: output[i] = (input[i][0], (input[i][1] / 2) + 1, (input[i][2] / 2) + 1) layers.append( convnet.PoolLayer(input[i], output[i], ds1, bias=True)) elif network_list[i] == "Relu": input[i] = output[i - 1] output[i] = input[i] layers.append(convnet.ReluLayer()) elif network_list[i] == "Affine": if i == 0: input[i] = (params_dict["InputChannel"], params_dict["InputWidth"], params_dict["InputHeight"]) else: input[i] = output[i - 1] if reshape_flag == True: input[i] = int(np.prod(input[i])) #output[i] = output_num[affine_cnt] output[i] = params_dict['Output' + str(i + 1)] layers.append( convnet.AffineLayer(input[i], output[i], T4toMat=reshape_flag)) reshape_flag = False #affine_cnt += 1 elif network_list[i] == "Softmax": layers.append(convnet.SoftmaxLayer()) cnn = CNN(layers) return cnn
def __init__(self, rng, batch_size, input): conv1_1 = layers.ConvLayer(rng, input=input, image_shape=(batch_size, 3, 480, 320), filter_shape=(32, 3, 3, 3), bias=True, padding='valid') conv1_2 = layers.ConvLayer(rng, input=K.spatial_2d_padding(conv1_1.output), image_shape=(batch_size, 32, 480, 320), filter_shape=(64, 32, 3, 3), padding='valid') maxp1 = layers.MaxPooling(input=K.spatial_2d_padding(conv1_2.output), poolsize=(2, 2)) conv2_1 = layers.ConvLayer(rng, input=maxp1.output, image_shape=(batch_size, 64, 240, 160), filter_shape=(64, 64, 3, 3), padding='valid') conv2_2 = layers.ConvLayer(rng, input=K.spatial_2d_padding(conv2_1.output), image_shape=(batch_size, 64, 240, 160), filter_shape=(64, 64, 3, 3), padding='valid') # maxp2 = layers.MaxPooling( # input=K.spatial_2d_padding(conv2_2.output), # poolsize=(2, 2) # ) # # conv3_1 = layers.ConvLayer( # rng, # input=maxp2.output, # image_shape=(batch_size, 10, 120, 80), # filter_shape=(10, 10, 3, 3), # padding='valid' # ) # # conv3_2 = layers.ConvLayer( # rng, # input=K.spatial_2d_padding(conv3_1.output), # image_shape=(batch_size, 10, 120, 80), # filter_shape=(10, 10, 3, 3), # padding='valid' # ) # # maxp3 = layers.MaxPooling( # input=conv3_2.output, # poolsize=(2, 2) # ) # # conv4_1 = layers.ConvLayer( # rng, # input=maxp3.output, # image_shape=(batch_size, 10, 60, 40), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # conv4_2 = layers.ConvLayer( # rng, # input=conv4_1.output, # image_shape=(batch_size, 10, 60, 40), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # maxp4 = layers.MaxPooling( # input=conv4_2.output, # poolsize=(2, 2) # ) # # conv5_1 = layers.ConvLayer( # rng, # input=maxp4.output, # image_shape=(batch_size, 10, 30, 20), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # conv5_2 = layers.ConvLayer( # rng, # input=conv5_1.output, # image_shape=(batch_size, 10, 30, 20), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # maxp5 = layers.MaxPooling( # input=conv5_2.output, # poolsize=(2, 2) # ) # # flat1_input = maxp5.output.flatten(2) # flat1_shape = 10*15*10 # # flat1 = layers.HiddenLayer( # rng=rng, # input=flat1_input, # n_in=flat1_shape, # n_out=flat1_shape, # activation=T.nnet.relu # ) # # flat2 = layers.HiddenLayer( # rng=rng, # input=flat1.output, # n_in=flat1_shape, # n_out=flat1_shape, # activation=T.nnet.relu # ) # # remax5_input = flat2.output.reshape((batch_size,10,15,10)) # # remax5 = layers.ReverseMaxPooling( # input=remax5_input, # mask=maxp5.mask, # poolsize=(2, 2) # ) # # deconv5_1 = layers.DeConvLayer( # rng, # #input=remax5.output, # input=conv5_2.output, # W=conv5_2.W, # image_shape=(batch_size, 10, 30, 20), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # deconv5_2 = layers.DeConvLayer( # rng, # input=deconv5_1.output, # W=conv5_1.W, # image_shape=(batch_size, 10, 30, 20), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # remax4 = layers.ReverseMaxPooling( # input=deconv5_2.output, # mask=maxp4.mask, # poolsize=(2, 2) # ) # # deconv4_1 = layers.DeConvLayer( # rng, # input=remax4.output, # W=conv4_2.W, # image_shape=(batch_size, 10, 60, 40), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # deconv4_2 = layers.DeConvLayer( # rng, # input=deconv4_1.output, # W=conv4_1.W, # image_shape=(batch_size, 10, 60, 40), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # remax3 = layers.ReverseMaxPooling( # input=deconv4_2.output, # mask=maxp3.mask, # poolsize=(2, 2) # ) # # deconv3_1 = layers.DeConvLayer( # rng, # #input=remax3.output, # input=conv3_2.output, # W=conv3_2.W, # image_shape=(batch_size, 10, 118, 78), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # deconv3_2 = layers.DeConvLayer( # rng, # input=K.spatial_2d_padding(deconv3_1.output), # W=conv3_1.W, # image_shape=(batch_size, 10, 120, 80), # filter_shape=(10, 10, 3, 3), # padding='same' # ) # # remax2 = layers.ReverseMaxPooling( # input=deconv3_2.output, # mask=maxp2.mask, # poolsize=(2, 2) # ) # deconv2_1 = layers.DeConvLayer( rng, #input=remax2.output, input=K.spatial_2d_padding(conv2_2.output), W=conv2_2.W, image_shape=(batch_size, 64, 240, 160), filter_shape=(64, 64, 3, 3), padding='same') deconv2_2 = layers.DeConvLayer(rng, input=deconv2_1.output, W=conv2_1.W, image_shape=(batch_size, 64, 240, 160), filter_shape=(64, 64, 3, 3), padding='same') remax1 = layers.ReverseMaxPooling(input=deconv2_2.output, mask=maxp1.mask, poolsize=(2, 2)) deconv1_1 = layers.DeConvLayer( rng, input=remax1.output, #input=K.spatial_2d_padding(conv1_2.output), W=conv1_2.W, image_shape=(batch_size, 64, 480, 320), filter_shape=(32, 64, 3, 3), padding='same', ) deconv1_2 = layers.ConvLayer(rng, input=deconv1_1.output, image_shape=(batch_size, 32, 480, 320), filter_shape=(1, 32, 3, 3), bias=True, padding='same', activation=T.nnet.sigmoid) self.y_pred = T.clip(deconv1_2.output, 0.001, 0.999) # self.params=conv1_1.params+conv1_2.params+conv2_1.params+conv2_2.params+\ # conv3_1.params+conv3_2.params+conv4_1.params+conv4_2.params+conv5_1.params+conv5_2.params+\ # deconv5_1.params+deconv5_2.params+deconv4_1.params+deconv4_2.params+deconv3_1.params+\ # deconv3_2.params+deconv2_1.params+deconv2_2.params+deconv1_1.params+deconv1_2.params self.params = conv1_1.params + conv1_2.params + conv2_1.params + conv2_2.params + deconv1_2.params self.input = input
from activation_functions import Sigmoid, Tanh, Linear from loss_functions import SoftMaxCrossEntropyLoss from mnist_data import x_train_2D as X from mnist_data import y_train_reformatted as y from mnist_data import x_test_2D as x_test from mnist_data import y_test_reformatted as y_test # =================================================================================== nn = NN.NN(loss_func=SoftMaxCrossEntropyLoss()) convLayer = layers.ConvLayer( n_channels=9, kernel_size=9, weight_init='glorot', activation_func=Tanh(), flatten=True, dropout=0.8, ) # pool = layers.PoolingLayer(pool_size=7, flatten=True) dense = layers.DenseLayer(10, Linear(), weight_initialisation='glorot') nn.add_layer(convLayer) # nn.add_layer(pool) nn.add_layer(dense) optimizer = optimizers.MomentumSGD(learning_rate=0.01, momentum=0.90) trainer = NN.Trainer(nn, optimizer) # statistic batch_size = 50
def __init__(self): self.convlayer = ly.ConvLayer() self.relu = ly.ReLU()