Python 我的神经网络总是输出相同的输出,而不管输入是什么
我试图创建一个神经网络,根据房子里卧室和浴室的数量来预测房子的价格。下面是“realestate.csv”文件 下面是我用python编写的代码Python 我的神经网络总是输出相同的输出,而不管输入是什么,python,pandas,numpy,neural-network,Python,Pandas,Numpy,Neural Network,我试图创建一个神经网络,根据房子里卧室和浴室的数量来预测房子的价格。下面是“realestate.csv”文件 下面是我用python编写的代码 import pandas as pd import numpy as np import matplotlib.pyplot as plt train_bed_bath = np.array(pd.read_csv('realestate.csv',usecols=['beds', 'baths']), dtype='int')[0:984] t
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
train_bed_bath = np.array(pd.read_csv('realestate.csv',usecols=['beds', 'baths']), dtype='int')[0:984]
train_price = np.array(pd.read_csv('realestate.csv',usecols=['price']),dtype='int')[0:984]
test_bed_bath = np.array(pd.read_csv('realestate.csv',usecols=['beds', 'baths']), dtype='int')[984:985]
predicted_outputs = []
class Neural_Network(object):
def __init__(self):
#parameters
self.inputSize = 2
self.hiddenSize = 3
self.outputSize = 1
#weights
self.W1 = np.random.randn(2, 3) # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to output layer
def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
self.o = self.sigmoid(self.z3) # final activation function
return self.o
def sigmoid(self, s):
# activation function
return .5 * (1 + np.tanh(.5 * s))
def sigmoidPrime(self, s):
#derivative of sigmoid
return s * (1 - s)
def backward(self, X, y, o):
# backward propagate through the network
self.o_error = y - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train(self, X, y):
self.o = self.forward(X)
self.backward(X, y, self.o)
NN = Neural_Network()
for i in range(1000):
NN.train(train_bed_bath, train_price)
for i in range(5):
for j in range (5):
print(NN.forward([i,j]))
它输出以下输出
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
我不知道它为什么这样做。任何帮助都将不胜感激 因为sigmoid函数的范围是[0,1],你得到的是一个回归问题,而不是分类问题。您不应该使用像sigmoid这样的激活函数。或者你可以把你的房价分成几类。你也可以试试线性regression@SamerAyoub我应该将所有价格除以数据中的最高价格吗?另外,您还将如何使用激活函数?在输出层中使用线性激活函数,在keras中,这将是激活='linear'
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]