首页 > 解决方案 > 无论输入如何,我的神经网络总是打印相同的输出

问题描述

我正在尝试创建一个神经网络,根据房子里的卧室和浴室的数量来预测房子的价格。下面是“realestate.csv”文件。

     beds  baths   price
0       2      1   59222
1       3      1   68212
2       2      1   68880
3       2      1   69307
4       2      1   81900
..    ...    ...     ...
980     4      3  232425
981     3      2  234000
982     3      2  235000
983     4      2  235301
984     3      2  235738

[985 rows x 3 columns]

下面是我在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]
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.]

我不确定它为什么这样做。任何帮助将不胜感激。

标签: pythonpandasnumpyneural-network

解决方案


推荐阅读