iT邦幫忙

0

請問各位大神要怎麼改?

  • 分享至 

  • xImage
import numpy
import scipy.special
import matplotlib.pyplot

def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
    self.inodes = inputnodes
    self.hnodes = hiddennodes
    self.onodes = outputnodes

    self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5),
        (self.hnodes, self.inodes))
    self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5),
        (self.onodes, self.hnodes))
    
    self.lr = learningrate

    self.activation_function = lambda x: scipy.special.expit(x)

    pass

def train(self, inputs_list, targets_list):
    inputs = numpy.array(inputs_list, ndmin=2).T
    targets = numpy.array(targets_list, ndmin=2).T

    hidden_inputs = numpy.dot(self.wih, inputs)

    hidden_outputs = self.activation_function(hidden_inputs)

    final_inputs = numpy.dot(self.who, hidden_outputs)
    
    final_outputs = self.activation_function(final_inputs)

    ouput_errors = (target - actual)

    hidden_errors = numpy.dot(self.who.T, outputs_errors)

    layers
    self.who += self.lr * numpy.dot((outputs_errors *
        final_outputs * (1.0 - final_outputs)),
        numpy.transpose(hidden_outputs))

    layers
    self.wih += self.lr * numpy.dot((hidden_errors *
        hidden_outputs * (1.0 - hidden_outputs)),
        numpy.transpose(inputs))

    pass
def query(self, inouts_list):
    inputs = numpy.array(inputs_list, ndmin=2).T

    hidden_inputs = numpy.dot(self.wih, inputs)

    hidden_outputs = self.ativation_function(hidden_inputs)

    final_inputs = numpy.dot(self.who, hidden_outputs)

    final_outputs = self.activation_function(final_inputs)

    return final_outputs

input_nodes = 784
hidden_nodes = 200
output_nodes = 10

learning_rate = 1.0

n = neuralNetwork(input_nodes, hidden_nodes, output_nodes,
    learning_rate)

training_data_file = open("mnist_dataset/mnist_train.csv", 'r')
training_data_list = training_data_file.readline()
training_data_file.close()

epochs = 5

for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:] / 255.0 * 0.99) + 0.01151)
        targets = numpy.zeros(output_nodes) + 0.01

        targets[int(all_values[0])] = 0.99
        n.train(inputs, targets)
        
        pass

pass

test_data_file = open("mnist_dataset/mnist_test.csv", 'r')
test_data_list = test_data_file.readline()
test_data_file.close()

for record in test_data_list:
    all_values = record.split(',')

    correct_label = int(all_values[0])

    inputs = (numpy.asfarray(all_values[1:] / 255.0 * 0.99) + 1.0)

    outputs = n.query(inputs)
              
    label = numpy.argmax(outputs)

    if (label == correct_label):
        scorecard.append(1)

    else:
        scorecard.append(0)
        pass
pass

scorecard_array = numpy.asarray(scorecard)
print("performmance = ", secordcard_array.sum()/scorecard_array.size)

https://ithelp.ithome.com.tw/upload/images/20200612/20127838GXnNGoxGXg.png

看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2020-06-12 21:42:08 檢舉
看一下 error message 跟你說啥
player iT邦大師 1 級 ‧ 2020-06-12 23:50:47 檢舉
neuralNetwork 未定義
誰知道你的 neuralNetwork 是哪來的?
小魚 iT邦大師 1 級 ‧ 2020-06-13 00:04:46 檢舉
應該是去哪裡複製貼上的吧,
結果少貼了一段,
或是對方沒有把那一段放上網.
我是按照書上打的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2020-06-13 00:19:05

改好了

import numpy
import scipy.special
import matplotlib.pyplot

def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
    self.inodes = inputnodes
    self.hnodes = hiddennodes
    self.onodes = outputnodes

    self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5),
        (self.hnodes, self.inodes))
    self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5),
        (self.onodes, self.hnodes))
    
    self.lr = learningrate

    self.activation_function = lambda x: scipy.special.expit(x)

    pass

def train(self, inputs_list, targets_list):
    inputs = numpy.array(inputs_list, ndmin=2).T
    targets = numpy.array(targets_list, ndmin=2).T

    hidden_inputs = numpy.dot(self.wih, inputs)

    hidden_outputs = self.activation_function(hidden_inputs)

    final_inputs = numpy.dot(self.who, hidden_outputs)
    
    final_outputs = self.activation_function(final_inputs)

    ouput_errors = (target - actual)

    hidden_errors = numpy.dot(self.who.T, outputs_errors)

    layers
    self.who += self.lr * numpy.dot((outputs_errors *
        final_outputs * (1.0 - final_outputs)),
        numpy.transpose(hidden_outputs))

    layers
    self.wih += self.lr * numpy.dot((hidden_errors *
        hidden_outputs * (1.0 - hidden_outputs)),
        numpy.transpose(inputs))

    pass
def query(self, inouts_list):
    inputs = numpy.array(inputs_list, ndmin=2).T

    hidden_inputs = numpy.dot(self.wih, inputs)

    hidden_outputs = self.ativation_function(hidden_inputs)

    final_inputs = numpy.dot(self.who, hidden_outputs)

    final_outputs = self.activation_function(final_inputs)

    return final_outputs

def neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate):
	return 0    

input_nodes = 784
hidden_nodes = 200
output_nodes = 10

learning_rate = 1.0

n = neuralNetwork(input_nodes, hidden_nodes, output_nodes,
    learning_rate)

training_data_file = open("mnist_dataset/mnist_train.csv", 'r')
training_data_list = training_data_file.readline()
training_data_file.close()

epochs = 5

for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:] / 255.0 * 0.99) + 0.01151)
        targets = numpy.zeros(output_nodes) + 0.01

        targets[int(all_values[0])] = 0.99
        n.train(inputs, targets)
        
        pass

pass

test_data_file = open("mnist_dataset/mnist_test.csv", 'r')
test_data_list = test_data_file.readline()
test_data_file.close()

for record in test_data_list:
    all_values = record.split(',')

    correct_label = int(all_values[0])

    inputs = (numpy.asfarray(all_values[1:] / 255.0 * 0.99) + 1.0)

    outputs = n.query(inputs)
              
    label = numpy.argmax(outputs)

    if (label == correct_label):
        scorecard.append(1)

    else:
        scorecard.append(0)
        pass
pass

scorecard_array = numpy.asarray(scorecard)
print("performmance = ", secordcard_array.sum()/scorecard_array.size)
看更多先前的回應...收起先前的回應...
ckp6250 iT邦好手 1 級 ‧ 2020-06-13 17:22:15 檢舉

果然指名大神,
大神就來啦。

thanks

dragonH iT邦超人 5 級 ‧ 2020-06-13 21:43:16 檢舉

.. 沒人懂海綿寶寶的梗嗎

/images/emoticon/emoticon17.gif

/images/emoticon/emoticon37.gif

解決問題最快的方法就是處理掉有問題的人

我要發表回答

立即登入回答