def test_simple_perseptron_predict(): sp = SimplePerceptron(3,1,threshold=1.0) assert sp.predict([0,0,0]) == [0] assert sp.predict([0,0,1]) == [1] assert sp.predict([0,1,0]) == [1] assert sp.predict([1,0,0]) == [1] assert sp.predict([0,1,1]) == [1] assert sp.predict([1,0,1]) == [1] assert sp.predict([1,1,0]) == [1] assert sp.predict([1,1,1]) == [1]
def test_simple_perseptron_train(): sp = SimplePerceptron(3,1, threshold=0.5, learning_coefficient=0.5) train_data = [ [[0,0,0],[1]],] sp.train(train_data) assert sp.weight_mat[0].tolist() == [0, 1, 1, 1] train_data = [ [[0,0,1],[0]], ] sp.train(train_data) assert sp.weight_mat[0].tolist() == [-0.5, 1, 1, 0.5] train_data = [ [[0,1,0],[0]], ] sp.train(train_data) assert sp.weight_mat[0].tolist() == [-1, 1, 0.5, 0.5] train_data = [ [[1,0,0],[0]], ] sp.train(train_data) assert sp.weight_mat[0].tolist() == [-1.5,0.5,0.5, 0.5]
def simple_perceptron(): # simple perceptron sp = SimplePerceptron( input_neuron_num = 2, output_neuron_num = 1, threshold = 1.0, learning_coefficient = 0.5, output_function_type = 1) # train data x_range = [0,30] y_range = [0,30] liner_data = TrainingData.liner_training_data(x_range, y_range) train_data = TrainingData.change_format(liner_data) # 教師データのプロット fig = plt.figure() scat(fig, [key for key, value in liner_data.items() if value == 0], color='g' ) scat(fig, [key for key, value in liner_data.items() if value == 1], color='b' ) # 学習前分離線 data = get_predict_list(x_range,y_range, sp) plot(fig, data) # 学習 sample_border = len(train_data) / 3 random.shuffle(train_data) for i in range(100): sp.train(train_data[:sample_border]) # 学習後分離線 data = get_predict_list(x_range,y_range, sp) plot(fig, data) plt.show()
def simple_sample(): sp = SimplePerceptron( input_neuron_num = 2, output_neuron_num = 1, threshold = 1.0, learning_coefficient = 0.5, output_function_type = 0) train_data = [ [[0,0,],[0]], [[0,1,],[1]], [[1,0,],[0]], [[1,1,],[1]], ] print '---- before' print sp.predict([0,0,]) print sp.predict([0,1,]) print sp.predict([1,0,]) print sp.predict([1,1,]) for i in range(100): sp.train(train_data) print '---- after' print sp.predict([0,0,]) print sp.predict([0,1,]) print sp.predict([1,0,]) print sp.predict([1,1,])