Python 如何更改神经网络中的数据类型
我是神经网络的新手,发现了这个神经网络的代码Python 如何更改神经网络中的数据类型,python,numpy,neural-network,Python,Numpy,Neural Network,我是神经网络的新手,发现了这个神经网络的代码 import numpy as np import pandas as pd # Activation function def sigmoid(t): return 1 / (1 + np.exp(-t)) # Derivative of sigmoid def sigmoid_derivative(p): return p * (1 - p) # Class definition class NeuralNetwork:
import numpy as np
import pandas as pd
# Activation function
def sigmoid(t):
return 1 / (1 + np.exp(-t))
# Derivative of sigmoid
def sigmoid_derivative(p):
return p * (1 - p)
# Class definition
class NeuralNetwork:
def __init__(self, x, y):
self.input = x
self.weights1 = np.random.rand(self.input.shape[1], 4) # considering we have 4 nodes in the hidden layer
self.weights2 = np.random.rand(4, 1)
self.y = y
self.output = np.zeros(y.shape)
def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weights1))
self.layer2 = sigmoid(np.dot(self.layer1, self.weights2))
return self.layer2
def backprop(self):
d_weights2 = np.dot(self.layer1.T, 2 * (self.y - self.output) * sigmoid_derivative(self.output))
d_weights1 = np.dot(self.input.T, np.dot(2 * (self.y - self.output) * sigmoid_derivative(self.output),
self.weights2.T) * sigmoid_derivative(self.layer1))
self.weights1 += d_weights1
self.weights2 += d_weights2
def train(self, X, y):
self.output = self.feedforward()
self.backprop()
if __name__ == "__main__":
X = np.array(([0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]), dtype=float)
y = np.array(([0], [1], [1], [0]), dtype=float)
NN = NeuralNetwork(X, y)
for i in range(1500): # trains the NN 1,000 times
if i % 100 == 0:
print("for iteration # " + str(i) + "\n")
print("Input : \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n" + str(NN.feedforward()))
print("Loss: \n" + str(np.mean(np.square(y - NN.feedforward())))) # mean sum squared loss
print("\n")
NN.train(X, y)
电流输出:
# for iteration # 1400
#
# Input :
# [[0. 0. 1.]
# [0. 1. 1.]
# [1. 0. 1.]
# [1. 1. 1.]]
# Actual Output:
# [[0.]
# [1.]
# [1.]
# [0.]]
# Predicted Output:
# [[0.01013418]
# [0.97148752]
# [0.97080865]
# [0.03590563]]
# Loss:
# 0.00076425301653189
我想将此浮点数从0改为1,改为int64
dtype numbers,以便:
X = np.array(([3, 5, 8, 10], [3, 5, 8, 10], [3, 5, 8, 10], [3, 5, 8, 10]), dtype=np.int64)
y = np.array(([3], [5], [8], [10]), dtype=np.int64)
我该怎么做:
float
更改为np.int64
# Input :
# [[ 3 5 8 10]
# [ 3 5 8 10]
# [ 3 5 8 10]
# [ 3 5 8 10]]
# Actual Output:
# [[ 3]
# [ 5]
# [ 8]
# [10]]
# Predicted Output:
# [[0.99999998]
# [0.99999998]
# [0.99999998]
# [0.99999998]]
# Loss: 37.500000214318064
所以我改变了:
NeuralNetwork
构造函数中将np.random.rand
更改为np.random.randint
Traceback (most recent call last):
File "E:/NeuralNetwork/main.py", line 54, in <module>
NN = NeuralNetwork(X, y)
File "E:/NeuralNetwork/main.py", line 28, in __init__
self.weights1 = np.random.randint(self.input.shape[1], 4) # considering we have 4 nodes in the hidden layer
File "mtrand.pyx", line 745, in numpy.random.mtrand.RandomState.randint
File "_bounded_integers.pyx", line 1363, in numpy.random._bounded_integers._rand_int32
ValueError: low >= high
回溯(最近一次呼叫最后一次):
文件“E:/NeuralNetwork/main.py”,第54行,在
NN=神经网络(X,y)
文件“E:/NeuralNetwork/main.py”,第28行,在初始化中__
self.weights1=np.random.randint(self.input.shape[1],4)#考虑到隐藏层中有4个节点
文件“mtrand.pyx”,第745行,位于numpy.random.mtrand.RandomState.randint中
文件“\u bounded\u integers.pyx”,第1363行,单位为numpy.random.\u bounded\u integers.\u rand\u int32
ValueError:低>=高
我应该如何替换整数数据中的浮点数进行预测?sigmoid输出层只能返回
[0,1]
中的值,因此至少您必须更改输出层的激活功能。sigmoid输出层只能返回[0,1]
中的值,因此,至少您必须更改输出层的激活功能。