Skip to content

Instantly share code, notes, and snippets.

@run2
Created January 28, 2015 13:36
Show Gist options
  • Save run2/86d18cfbc1a15a05511f to your computer and use it in GitHub Desktop.
Save run2/86d18cfbc1a15a05511f to your computer and use it in GitHub Desktop.
Nolearn net to do supervised learning
This is an example nolearn NeuralNet to do supervised learning. This network is being used to do supervised learning on black and white images, which are 135 * 240 in size. The same network can be modified to bgr images just by modifying the input_shape to None,3,..,.. and output_num_unit to 3*135 * 240
net_unsupervised_bw = NeuralNet(
layers=[
('input', layers.InputLayer),
('noise1', GaussianNoiseLayer),
('conv1', Conv2DLayer),
('pool1', MaxPool2DLayer),
('dropout1', layers.DropoutLayer), # !
('conv2', Conv2DLayer),
('pool2', MaxPool2DLayer),
('dropout2', layers.DropoutLayer), # !
('conv3', Conv2DLayer),
('pool3', MaxPool2DLayer),
('dropout3', layers.DropoutLayer), # !
('hidden1', layers.DenseLayer),
('hidden2', layers.DenseLayer),
('hidden3', layers.DenseLayer),
('output', layers.DenseLayer),
],
batch_iterator=BatchIterator(20,True),
input_shape=(None, 1, 135, 240),
conv1_num_filters=32, conv1_filter_size=(12, 21), pool1_ds=(2, 2),
dropout1_p=0.1, # !
conv2_num_filters=64, conv2_filter_size=(7,11), pool2_ds=(2, 2),
dropout2_p=0.2, # !
conv3_num_filters=128, conv3_filter_size=(5,9), pool3_ds=(2, 2),
dropout3_p=0.3, # !
hidden3_num_units=100, hidden3_nonlinearity=sigmoid,
hidden1_num_units=100, hidden1_nonlinearity=sigmoid,
hidden2_num_units=100, hidden2_nonlinearity=sigmoid,
output_num_units=135*240, output_nonlinearity=None,
on_epoch_finished=[
#AdjustVariable('update_learning_rate', start=0.03, stop=0.0001),
#AdjustVariable('update_momentum', start=0.9, stop=0.999),
EarlyStopping(patience=20),
],
#updated=adagrad,
update_learning_rate=0.03,
update_momentum=0.95,
regression=True,
max_epochs=60,
verbose=1,
)
def load_data(dataset):
# Load the dataset
f = open(dataset, 'rb')
dump = cPickle.load(f)
train_set = dump['train']
#valid_set = dump['validate']
#test_set = dump['test']
f.close()
def get_dataset(data_xy, borrow=True):
data_x, data_y = data_xy
x = np.asarray(data_x,dtype=np.float32)/255.
y = np.asarray(data_x,dtype=np.float32)/255.
return x , y
#test_set_x, test_set_y = get_dataset(test_set)
#valid_set_x, valid_set_y = get_dataset(valid_set)
train_set_x, train_set_y = get_dataset(train_set)
rval = [(train_set_x, train_set_y)]#,(test_set_x, test_set_y)] #, (valid_set_x, valid_set_y),
return rval
datasets = load_data(TRAINFILE)
X, y = datasets[0]
print("X.shape == {}; X.min == {:.3f}; X.max == {:.3f}".format(
X.shape, X.min(), X.max()))
print("y.shape == {}; y.min == {:.3f}; y.max == {:.3f}".format(
y.shape, y.min(), y.max()))
if(bw=='color'):
X = X.reshape(-1, 3, 135, 240)
y = y.reshape(-1, 3 * 135 * 240)
else:
X = X.reshape(-1, 1, 135, 240)
y = y.reshape(-1, 135 * 240)
print("X.shape == {}; X.min == {:.3f}; X.max == {:.3f}".format(
X.shape, X.min(), X.max()))
print("y.shape == {}; y.min == {:.3f}; y.max == {:.3f}".format(
y.shape, y.min(), y.max()))
net = net_unsupervised_bw
if(bw=='color'):
net.input_shape=(None, 3, 135, 240)
net.output_num_units=3* 135 * 240
net.max_epochs = int(epochs)
net.loss = mse
net.fit(X, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment