epoch_loss += loss.loss(predicted, batch.targets) grad = loss.grad(predicted, batch.targets) net.backward(grad) optimizer.step(net) print(epoch, epoch_loss) net = NeuralNet([ Convolution_2D(name="conv_1", filter_shape=(10,1,3,3),padding="same",stride=1), Avg_Pool_2D(name="avg_pool_1", size=2, stride=2), SpatialBatchNormalization(name="sbn_1",input_channel=10), ReLU(name="relu_1"), Convolution_2D(name="conv_2", filter_shape=(20,10,3,3),padding="same",stride=1), Avg_Pool_2D(name="avg_pool_2", size=2, stride=2), SpatialBatchNormalization(name="sbn_2",input_channel=20), ReLU(name="relu_2"), Flatten(name="flat_1"), Dense(input_size=15*40*20, output_size=100, name="dense_1"), BatchNormalization(name="bn_1",input_size=100), ReLU(name="relu_3"), Dense(input_size=100, output_size=40, name="dense_2"), BatchNormalization(name="bn_2",input_size=40), Sigmoid(name="sigmoid_1") ]) train(net, num_epochs = 500)
import numpy as np from deeplearning.nn import NeuralNet from deeplearning.activation import Tanh, Sigmoid from deeplearning.layers import Dense from deeplearning.train import train inputs = np.array([[0, 0], [1, 0], [0, 1], [1, 1], [2, 2]]) targets = np.array([[0], [1], [1], [2], [6]]) net = NeuralNet([ Dense(name="dense_1", input_size=2, output_size=50), Sigmoid(name="sigmoid_1"), Dense(name="dense_2", input_size=50, output_size=1) ]) train(net, inputs, targets, num_epochs=10000) for x, y in zip(inputs, targets): predicted = net.forward(x, training=False) print(x, predicted, y)
print("=" * 30) print("Checking GPU cards, will use first GPU") gpus = GPUtil.getGPUs() for i, d in enumerate(gpus): print("\tid: %r" % d.id) print("\tmemoryFree: %r" % d.memoryFree) print("\tmemoryTotal: %r" % d.memoryTotal) print("\tdriver: %r" % d.driver) print("\tname: %r" % d.name) if i == 0: opts["memtot"] = d.memoryTotal opts["memfree"] = d.memoryFree print("Saving options. Ready to preprocess data") os.system("rm -rf runs/ ; mkdir runs") json.dump(opts, open("runs/options.json", "w")) print("=" * 30) print("Starting slicing of data and packing") import prepdata.slicedata, prepdata.packdata prepdata.slicedata.slice(csvfile, pdir) prepdata.packdata.packdata(csvfile) os.environ["CUDA_VISIBLE_DEVICES"] = "0" from deeplearning.train import train train(30) from deeplearning.render import render render("runs/history.txt", "runs/learning.html")
import numpy as np from deeplearning.train import train from deeplearning.nn import NeuralNet from deeplearning.activation import Tanh, Softmax, Sigmoid, ReLU from deeplearning.layers import Dense, Dropout from deeplearning.loss import CrossEntropy from deeplearning.optim import Momentum_SGD inputs = np.array([[0, 0], [0, 1], [1, 1], [1, 0]]) targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) net = NeuralNet([ Dense(input_size=2, output_size=2, name="dense_1"), Softmax(name="softmax_1"), ]) train(net, inputs, targets, num_epochs=1000, loss=CrossEntropy(), optimizer=Momentum_SGD()) # train(net, inputs, targets, num_epochs=3000) for x, y in zip(inputs, targets): predicted = net.forward(x, training=False) print(x, predicted, y)
mnist["test_images"] = mnist["test_images"].reshape((10000,1,28,28)) mnist["training_labels"] = one_hot(mnist["training_labels"]) mnist["test_labels"] = one_hot(mnist["test_labels"]) return mnist dataset = load_data() net = Sequential( name = "residual_net", layers = [ res_block(name="res_block_1",n_channels=1,n_out_channels=5,stride=2), res_block(name="res_block_2",n_channels=5,n_out_channels=5,stride=1), Flatten(name="flat_1"), Dense(input_size=14*14*5, output_size=10, name="dense_1"), BatchNormalization(name="bn_1",input_size=10), Softmax(name="softmax_1") ]) train(net, dataset["test_images"][1000:5000], dataset["test_labels"][1000:5000], num_epochs=20,loss=CrossEntropy(),optimizer=Adam()) y_test = np.argmax(dataset["test_labels"][0:1000],axis=1) print(accurarcy(net.predict(dataset["test_images"][0:1000]), y_test)) for map_name,name,param,grad in net.get_params_grads(): print(map_name,",",name)
Dense(input_size=12, output_size=50, name="dense_1"), BatchNormalization(input_size=50, name="bn_1"), ReLU(name="relu_1"), Dense(input_size=50, output_size=100, name="dense_2"), BatchNormalization(input_size=100, name="bn_2"), ReLU(name="relu_2"), Dense(input_size=100, output_size=2, name="dense_4"), BatchNormalization(input_size=2, name="bn_4"), Softmax(name="softmax_1") ]) #net = NeuralNet([ # Dense(input_size=12, output_size=50,name="dense_1",regularizer=L2_Regularization(lamda=0.003)), # ReLU(name="relu_1"), # Dense(input_size=50, output_size=100,name="dense_2",regularizer=L2_Regularization(lamda=0.003)), # ReLU(name="relu_2"), # Dense(input_size=100, output_size=2,name="dense_3",regularizer=L2_Regularization(lamda=0.003)), # ReLU(name="relu_3"), # Softmax(name="softmax_1") #]) train(net, x_train, y_train, num_epochs=1000, loss=CrossEntropy(), optimizer=SGD()) y_test = np.argmax(y_test, axis=1) print(accurarcy(net.predict(x_test), y_test))
from deeplearning.activation import Tanh, Softmax, Sigmoid, ReLU from deeplearning.layers import Dense, Dropout, Flatten from deeplearning.rnn import RNN, LastTimeStep from deeplearning.loss import CrossEntropy, MSE from deeplearning.optim import SGD, Adam net = Sequential(name="net", layers=[ RNN(name="rnn_1", D=8, H=16), Sigmoid(name="sigmoid_1"), LastTimeStep(name="last_1"), Dense(name="dense_1", input_size=16, output_size=8), Sigmoid(name="sigmoid_5") ]) train(net, X, y, num_epochs=1000, loss=MSE(), optimizer=Adam()) for map_name, name, param, grad in net.get_params_grads(): print(map_name, ",", name) def binary2int(x): res = 0 for i in range(x.shape[0]): res *= 2 res += x[i] return res target = y