numpy点积返回[inf]

时间:2019-01-25 12:39:02

标签: python-3.x numpy for-loop numpy-ndarray

我是python的新手,并且正在学习深度学习,因此我使用python创建了基本的神经网络代码。有一个神经元的输入值为(1到49),单个输出为sin(input)。我所做的所有事情如下:

输入为:

array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.],.....[49]]

输出为:

array([[ 0.84147098],
       [ 0.90929743],
       [ 0.14112001],
       [-0.7568025 ],.....]

完整代码:

import numpy as np
import pandas as pd
data=pd.read_excel("C:/Users/neural_network.xlsx")
total_rows=data.shape[0]
np.seterr(divide='ignore', invalid='ignore') 

def sigmoid(x):
    d=(np.exp(x)/(1+np.exp(x)))
    return d

def derivative_sigmoid(x):
    derivative=x/(1-x)
    return derivative


epoch=5000
hidden_layer=3
input_neurons=1
output_neurons=1
learning_rate=0.1
input_array=data['input'].values.reshape(total_rows,1)
input_array=input_array.astype(np.float64)
output_array=data['output'].values.reshape(total_rows,1)
output_array=output_array.astype(np.float64)

weights_in=np.random.uniform(size=(input_neurons,hidden_layer)) 
weights_in=weights_in.astype(np.float64)
bias_in=np.random.uniform(size=(1,hidden_layer))
bias_in=bias_in.astype(np.float64)
weights_out=np.random.uniform(size=(hidden_layer,output_neurons))
weights_out=weights_out.astype(np.float64)
bias_out=np.random.uniform(size=(1,output_neurons))
bias=weights_in.astype(np.float64)

for i in range(epoch):

    #forward propogation
    hidden_layer_output=(np.dot(input_array,weights_in))+bias_in
    activation_1=sigmoid(hidden_layer_output)
    activation_2_input=(np.dot(activation_1,weights_out))+bias_out
    predicted_output=sigmoid(activation_2_input)


    # #backward propogation

    Error=(predicted_output-output_array)

    rate_change_output=derivative_sigmoid(predicted_output)
    rate_change_hidden_output=derivative_sigmoid(activation_1)
    error_on_output=Error*rate_change_output
    error_hidden_layer=error_on_output.dot(weights_out.T)
    delta_hidden_layer=error_hidden_layer*rate_change_hidden_output
    weights_out+=activation_1.T.dot(error_on_output)*learning_rate
    weights_in+=input_array.T.dot(delta_hidden_layer)*learning_rate
    bias_out+=np.sum(error_on_output,axis=0,keepdims=True)*learning_rate
    bias_in+=np.sum(error_hidden_layer,axis=0,keepdims=True)*learning_rate  

print (weights_out)

现在,当我尝试不使用weights_out=activation_1.T.dot(error_on_output)*learning_rate打印for loop时,它将为我提供weights_out的正确更新值。但是,由于我在for loop中使用相同的代码进行迭代等于epoch,所以它返回为:

[[nan]
 [nan]
 [nan]]

我分别尝试了每一行并获得正确的值,还将dtype更改为'float64',但是代码似乎无法在for循环内运行。循环有问题吗?

1 个答案:

答案 0 :(得分:1)

import 'ol/ol.css'; import Map from 'ol/Map'; import View from 'ol/View'; import MVT from 'ol/format/MVT'; import VectorTileLayer from 'ol/layer/VectorTile'; import VectorTileSource from 'ol/source/VectorTile'; import TileLayer from 'ol/layer/Tile'; import XYZSource from 'ol/source/XYZ'; import Overlay from 'ol/Overlay'; import { Style, Fill, Stroke, Circle, Text } from 'ol/style'; import { fromLonLat } from 'ol/proj'; const map = new Map({ target: 'map-container', view: new View({ center: fromLonLat([34.633623, 39.818770]), zoom: 7 }) }); const layer = new VectorTileLayer({ source: new VectorTileSource({ attributions: [ '<a href="http://www.openmaptiles.org/" target="_blank">&copy; OpenMapTiles</a>', '<a href="http://www.openstreetmap.org/about/" target="_blank">&copy; OpenStreetMap contributors</a>' ], format: new MVT(), url: `http://localhost:8090/maps/observation/{z}/{x}/{y}.vector.pbf`, maxZoom: 24, type: 'base' }) }); const baseLayer = new TileLayer({ source: new XYZSource({ url: 'http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg' }), type: 'base' }); map.addLayer(baseLayer); const overlay = new Overlay({ element: document.getElementById('popup-container'), positioning: 'bottom-center', offset: [0, -10], autoPan: true }); map.addOverlay(overlay); overlay.getElement().addEventListener('click', function () { overlay.setPosition(); }); map.addLayer(layer); layer.setStyle(function (feature, resolution) { const properties = feature.getProperties(); if (properties.layer == 'temperature_stations' || properties.layer == 'temperature_stations_simple') { const point = new Style({ image: new Circle({ radius: 5, fill: new Fill({ color: 'red' }), stroke: new Stroke({ color: 'grey' }) }) }) return point } if (properties.layer == 'aws_stations') { const point = new Style({ image: new Circle({ radius: 5, fill: new Fill({ color: 'blue' }), stroke: new Stroke({ color: 'grey' }) }) }) return point } if (properties.layer == 'spa_stations') { const point = new Style({ image: new Circle({ radius: 10, fill: new Fill({ color: 'green' }), stroke: new Stroke({ color: 'grey' }) }) }) return point } if (properties.layer == 'syn_stations') { const point = new Style({ image: new Circle({ radius: 10, fill: new Fill({ color: 'purple' }), stroke: new Stroke({ color: 'grey' }) }) }) return point } }); map.on('pointermove', function (e) { let markup = ''; map.forEachFeatureAtPixel(e.pixel, function (feature) { markup += `${markup && '<hr>'}<table>`; const properties = feature.getProperties(); for (const property in properties) { markup += `<tr><th>${property}</th><td>${properties[property]}</td></tr>`; } markup += '</table>'; }, { hitTolerance: 0 }); if (markup) { document.getElementById('popup-content').innerHTML = markup; overlay.setPosition(e.coordinate); } else { overlay.setPosition(); } }); 的输出变得非常大(> 20)。然后,您将应用S型函数,并且hidden_layer_output中的值越来越接近。实际上,距离计算机太近,无法区分值和1。

在反向传播期间计算activation_1时,您正在计算这些值的rate_change_hidden_output。因此,您正在计算nan的1 /(1-1)抽动结果。

这些nan值然后传播到所有值,导致所有值均为nan。