def run(self): getdataob = GetDataSet() X_train, Y_train, X_test, Y_test = getdataob.createDataSet(9) self.n_classes = len(numpy.unique(Y_train)) # Create CNN Feature Extractor nn = NeuralNetwork( layers=[ #CNN Feature Extractor Conv(n_filters=4, filter_shape=(3, 3), weight_scale=0.1, weight_decay=0.01), Activation('relu'), Pool(pool_shape=(2, 2), mode='max'), Conv(n_filters=8, filter_shape=(3, 3), weight_scale=0.1, weight_decay=0.01), Activation('relu'), Pool(pool_shape=(2, 2), mode='max'), Flatten(), # Gives Feature Vectors # Classifier Linear(n_out=self.n_classes, weight_scale=0.1, weight_decay=0.002), LogRegression(), ], ) # Initialize(Setup) The Layers of CNN nn._setup(X_train, Y_train) #Fit the Training Set to CNN to learn the task specific filters for Feature Extraction nn.fit(X_train, Y_train, learning_rate=0.01, max_iter=100, batch_size=40) print("\nModel Trained\n\n") print("\nTesting Prediction : \n") Y_pred = nn.predict(X_test) print("Actual : \n", Y_test) print("Predicted : \n", Y_pred) error = nn._error(Y_pred, Y_test) print("Testing Error : ", error) file = open("weights.txt", "w") file.write("\nTesting Error : " + str(error))
def cross_validate(X, Y, n_folds, params): X_folds, Y_folds = create_folds(X, Y, n_folds) mask = np.array([True for x in xrange(n_folds)]) accuracy = [] for i in xrange(n_folds): mask[i] = False train_X = np.concatenate(X_folds[mask]) train_Y = np.concatenate(Y_folds[mask]) ann = NeuralNetwork(**params) ann.fit(train_X, train_Y) mask[i] = True fold_accuracy = ann.score(X_folds[i], Y_folds[i]) accuracy.append(fold_accuracy) accuracy = np.array(accuracy) print params, 'val accuracy: ', accuracy.mean() #, '+/-', 2*accuracy.std() return accuracy.mean(), params
from neuralnetwork import NeuralNetwork import numpy as np nn = NeuralNetwork([2, 2, 1], "tanh") x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 1, 1, 0]) nn.fit(x, y) for i in [[0, 0], [0, 1], [1, 0], [1, 1]]: print(i, nn.predict(i))
from neuralnetwork import NeuralNetwork import numpy as np nn = NeuralNetwork([2, 2, 1], 'tanh') X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 1, 1, 0]) nn.fit(X, y) for i in ([[0, 0], [0, 1], [1, 0], [1, 1]]): print(i, nn.predict(i))
# pixel intensity values to the range [0, 1] (each image is represented # by an 8x8 = 64-dim feature vector) print("[INFO] loading MINST (sample) dataset...") digits = datasets.load_digits() data = digits.data.astype("float") data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) # construct the test and training splits (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) # train the network print("[INFO] training network...") nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print("[INFO] {}".format(nn)) nn.fit(trainX, trainY, epochs=1000) print("[INFO] evaluating network...") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
from sklearn.metrics import confusion_matrix, classification_report from sklearn.preprocessing import LabelBinarizer from neuralnetwork import NeuralNetwork from sklearn.cross_validation import train_test_split digits = load_digits() X = digits.data # print(X) y = digits.target # print(y) X -= X.min() X /= X.max() # print(X) nn = NeuralNetwork([64, 100, 10], 'logistic') X_train, X_test, y_train, y_test = train_test_split(X, y) lables_train = LabelBinarizer().fit_transform(y_train) lables_test = LabelBinarizer().fit_transform(y_test) print("start fitting") nn.fit(X_train, lables_train, epochs=3000) predictions = [] for i in range(X_test.shape[0]): o = nn.predict(X_test[i]) predictions.append(np.argmax(o)) print(predictions) #混淆矩阵是一种特定的矩阵用来呈现算法性能的可视化效果 print(confusion_matrix(y_test, predictions)) # sklearn中的classification_report函数用于显示主要分类指标的文本报告.在报告中显示每个类的精确度,召回率,F1值等信息。 print(classification_report(y_test, predictions))
def week_6_main(): # test_row = np.array([[1, 2]]) # test_targets = [1] # accuracy_record = np.array([['epoch num', 'accuracy']]) shell = Shell() # # part 1, iris dataset print("iris data output") shell.data_set, shell.data_targets = shell.file_reader.read_data_from_file( "iris.csv") shell.prepare_data_set(shell.data_set, shell.data_targets) network = NeuralNetwork(num_layers=2, nodes_per_layer=[2, 3], learning_rate=.2) network.generate_weights(num_inputs=shell.training_data.shape[1]) for i in range(50): p_targets = network.fit(shell.training_data, shell.training_targets) # epoch_accuracy = shell.determine_training_accuracy(p_targets) # accuracy_record = np.append(accuracy_record, [[i, epoch_accuracy]], axis=0) # record the data to csv file # accuracy_df = pandas.DataFrame(data=list(accuracy_record[1:]), columns=list(accuracy_record[0])) # print(accuracy_df) # accuracy_df.to_csv("NN_accuracy.csv", index=False) shell.predicted_targets = network.fit(shell.test_data, shell.test_targets) shell.determine_accuracy() print("sklearn implementation for iris") sklearn_MLP = MLPClassifier(hidden_layer_sizes=(2, 3), solver='sgd', learning_rate_init=.2, max_iter=1000) sklearn_MLP.fit(shell.training_data, shell.training_targets) shell.predicted_targets = sklearn_MLP.predict(shell.test_data) shell.determine_accuracy() # part 2 diabetes dataset print("Diabetes Data output:") shell.data_set, shell.data_targets = shell.file_reader.read_diabetes_data() shell.prepare_data_set(shell.data_set, shell.data_targets) network = NeuralNetwork(num_layers=4, nodes_per_layer=[8, 4, 3, 2], learning_rate=.2) network.generate_weights(num_inputs=shell.training_data.shape[1]) for i in range(200): p_targets = network.fit(shell.training_data, shell.training_targets) # epoch_accuracy = shell.determine_training_accuracy(p_targets) # accuracy_record = np.append(accuracy_record, [[i, epoch_accuracy]], axis=0) # record to csv # accuracy_df = pandas.DataFrame(data=list(accuracy_record[1:]), columns=list(accuracy_record[0])) # print(accuracy_df) # accuracy_df.to_csv("PIMA_accuracy.csv", index=False) shell.predicted_targets = network.fit(shell.test_data, shell.test_targets) shell.determine_accuracy() print("sklearn implementation") sklearn_MLP = MLPClassifier(hidden_layer_sizes=(8, 4, 3, 2), solver='sgd', learning_rate_init=.2, max_iter=10000) sklearn_MLP.fit(shell.training_data, shell.training_targets) shell.predicted_targets = sklearn_MLP.predict(shell.test_data) shell.determine_accuracy()
from neuralnetwork import NeuralNetwork import numpy as np X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) nn = NeuralNetwork([2, 2, 1], alpha=.5) nn.fit(X, y, epochs=20000) for (x, target) in zip(X, y): pred = nn.predict(x)[0][0] step = 1 if pred > 0.5 else 0 print("[INFO] data={}, ground-truth={}, pred={:.4f}, step={}".format( x, target[0], pred, step))
from neuralnetwork import NeuralNetwork import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt # 生成的数据 X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y_or = np.array([[0], [1], [1], [1]]) y_and = np.array([[0], [0], [0], [1]]) y_xor = np.array([[0], [1], [1], [0]]) # 构造2-2-1结构的神经网络,2个节点输入层,2个节点的隐藏层,1个节点的输出层 nn = NeuralNetwork([2, 2, 1], alpha=0.5) # 模型开始训练,更新得到最终不断迭代更新的weigh矩阵 losses = nn.fit(X, y_xor, epochs=2) # 打印输出 for (x, target) in zip(X, y_xor): pred = nn.predict(x)[0][0] step = 1 if pred > 0.5 else 0 print("[INFO] data-{}, ground_truth={}, pred={:.4f}, step={}".format( x, target[0], pred, step)) print("X.shape\n", X.shape) print("y_xor.shape\n", y_xor.shape) # 可视化训练过程 plt.style.use("ggplot") plt.figure() plt.title("Data") cm_dark = mpl.colors.ListedColormap(['g', 'b']) plt.scatter(X[:, 0], X[:, 1], marker="o", c=y_xor.ravel(), cmap=cm_dark, s=80) # print(testY)
from sklearn.preprocessing import LabelBinarizer from neuralnetwork import NeuralNetwork from sklearn.cross_validation import train_test_split from sklearn.datasets import load_digits import numpy as np from sklearn.metrics import confusion_matrix, classification_report digits = load_digits() x = digits.data y = digits.target x -= x.min() x /= x.max() nn = NeuralNetwork([64, 100, 10], "logistic") x_train, x_test, y_train, y_test = train_test_split(x, y) label_train = LabelBinarizer().fit_transform(y_train) label_test = LabelBinarizer().fit_transform(y_test) print("start fitting..") predictions = [] nn.fit(x_train, label_train, epochs=3000) for i in range(x_test.shape[0]): o = nn.predict(x_test[i]) predictions.append(np.argmax(o)) print(confusion_matrix(y_test, predictions)) print(classification_report(y_test, predictions))
Y = np.concatenate([Y, test_Y]) scores = [] for decay in np.arange(0, .2, .01): params = {'decay': decay} accuracy, params = cross_validate(X, Y, 3, params) momentum = params['decay'] scores.append((decay, accuracy)) scores = np.array(scores) plt.plot(scores[:, 0], scores[:, 1]) plt.xlabel('decay') plt.ylabel('cv accuracy') plt.show() ann = NeuralNetwork(verbose=True, lr=.15, decay=0.04, nesterov=True, momentum=.4, layer_1_size=64, layer_2_size=128, nb_epochs=10) ann.fit(X, Y, validation_split=.2) print ann.score(test_X, test_Y) predictions = ann.predict(test_X) np.savetxt('testX2predictions', predictions) ann.plot_history()
testY = LabelBinarizer().fit_transform(testY) print("Vectorize trainY:\n", trainY) print("trainY[0]\n", testY[0]) print("Vectroize testY:\n", testY) # 定义网络结构64-32-32-16-10,64表示输入层有64个nodes(因为8x8=64),输出层有10个nodes(10个数值0-9输出) print("[INFO] training network...") nn = NeuralNetwork([trainX.shape[1], 32, 32, 16, 10]) print("[INFO] {}".format(nn)) # print("trainX.shape[0]:\n", trainX.shape[0]) # print("trainX.shape:\n", trainX.shape) print("trainX.shape\n", trainX.shape) print("testY.shape\n", testY.shape) # 训练模型 losses = nn.fit(trainX, trainY, epochs=5000) # 预测,并生成报告 print("[INFO] evaluating network...") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions)) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, len(losses)), losses) plt.title("Training Loss") plt.xlabel("Epoch #") plt.ylabel("Loss") plt.show() print("W\n", nn.W)
from sklearn.metrics import classification_report from sklearn import datasets import numpy as np import matplotlib.pyplot as plt np.random.seed(24) X, y = datasets.make_moons(300, noise=0.50) #train network print("[INFO] training network...") #Here we can see that we are training a NeuralNetwork with a 64-32-16-10 architecture. #The output layer has ten nodes due to the fact that there are ten possible output classes for the digits #0-9. nn = NeuralNetwork([X.shape[1], 2, 1]) print("[INFO] {}".format(nn)) nn.fit(X, y, epochs=50) #Evaluating model on testing set print("[INFO] evaluating network...") predictions = nn.predict(X) #o find the class label with the largest probability for each data point, we use the argmax #function predictions = predictions.argmax(axis=1) print(classification_report(y, predictions)) plt.figure(figsize=(10, 7)) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral) plt.title('Dataset') plt.show() #plt.figure(figsize = (10,7))
# importing necessary packages from neuralnetwork import NeuralNetwork import numpy as np # construct the XOR dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) Y = np.array([[0], [1], [1], [0]]) # define our 2-2-1 neural network and train it nn = NeuralNetwork([2, 2, 1], alpha=0.5) nn.fit(X, Y, epochs=20000) # now that our network is trained, loop over the XOR data points for (x, targets) in zip(X, Y): # make the prediction on the data point and display the result # to the console pred = nn.predict(x)[0][0] step = 1 if pred > 0.5 else 0 print( "[INFO] data = {}, ground truth = {}, pred = {:.4f}, step = {}".format( x, targets[0], pred, step))
# construct the training and testing splits (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) # convert the labels from integers to vectors trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) # train the network print("[INFO] training network...") nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print("[INFO] {}".format(nn)) epoches = 1000 losses = nn.fit(trainX, trainY, epochs=epoches) # evaluate the network print("[INFO] evaluating network...") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions)) ''' # construct a figure that plots the loss over time plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, epoches), losses) plt.title("Training Loss") plt.xlabel("Epoch #") plt.ylabel("Loss") plt.show()
from sklearn.preprocessing import StandardScaler X = StandardScaler().fit_transform(X) # use my custom data splitter for training/test X_train, X_test, y_train, y_test = train_test_split(X, y, seed=0, test_size = 0.25) start_time = timeit.timeit() print "starting fit" nn = NeuralNetwork(n_iter = 2, n_print = 5, learning_rate = 1,\ num_hidden_units=24, seed=42, verbose = False) nn.fit(X_train, y_train) end_time = timeit.timeit() print "Fitting time: {}".format(end_time - start_time) print "W matrix (size = {} x {}) = {}".format(nn.W.shape[0],nn.W.shape[1],nn.W) print "V matrix (size = {} x {}) = {}".format(nn.V.shape[0],nn.V.shape[1],nn.V) np.set_printoptions(threshold=np.inf) y_pred = nn.predict(X_train) print "Training Accuracy score = {}".format(accuracy_score(y_train, y_pred)) y_pred = nn.predict(X_test) print "Test Accuracy score = {}".format(accuracy_score(y_test, y_pred)) y_prob = nn.predict_prob(X_test, add_bias_unit=True)