資源簡介
波士頓房價預(yù)測的BP神經(jīng)網(wǎng)絡(luò)實現(xiàn)
1) 訓(xùn)練數(shù)據(jù) housing.csv 使用波士頓房價數(shù)據(jù)
2) 使用Python代碼實現(xiàn)前向和后向傳播
3) 損失函數(shù)使用方差

代碼片段和文件信息
#?Package?imports
import?pandas?as?pd
import?numpy?as?np
import?matplotlib.pyplot?as?plt
def?clc_accuracy(y_true?y_predict):
????“““?use?sklearn?to?calcuate?the?R2?score“““
????from??sklearn.metrics?import?r2_score
????score?=?r2_score(y_true?y_predict)
????return?score
def?sigmoid(Z):
????“““
????Compute?the?sigmoid?of?x
????Arguments:
????x?--?A?scalar?or?numpy?array?of?any?size.
????Return:
????s?--?sigmoid(x)
????“““
????A?=?1.0/(1.0+np.exp(-Z))
????return?A
def?sigmoid_backward(dA?cache):
????“““
????Implement?the?backward?propagation?for?a?single?SIGMOID?unit.
????Arguments:
????dA?--?post-activation?gradient?of?any?shape
????cache?--?‘Z‘?where?we?store?for?computing?backward?propagation?efficiently
????Returns:
????dZ?--?Gradient?of?the?cost?with?respect?to?Z
????“““
????Z?=?cache
????s?=?1.0?/?(1.0?+?np.exp(-Z))
????dZ?=?dA?*?s?*?(1?-?s)
????assert?(dZ.shape?==?Z.shape)
????return?dZ
def?relu(Z):
????“““
????Implement?the?RELU?function.
????Arguments:
????Z?--?Output?of?the?linear?layer?of?any?shape
????Returns:
????A?--?Post-activation?parameter?of?the?same?shape?as?Z
????cache?--?a?python?dictionary?containing?“A“?;?stored?for?computing?the?backward?pass?efficiently
????“““
????A?=?np.maximum(0?Z)
????assert?(A.shape?==?Z.shape)
????return?A
def?relu_backward(dA?cache):
????“““
????Implement?the?backward?propagation?for?a?single?RELU?unit.
????Arguments:
????dA?--?post-activation?gradient?of?any?shape
????cache?--?‘Z‘?where?we?store?for?computing?backward?propagation?efficiently
????Returns:
????dZ?--?Gradient?of?the?cost?with?respect?to?Z
????“““
????Z?=?cache
????dZ?=?np.array(dA?copy=True)??#?just?converting?dz?to?a?correct?object.
????#?When?z?<=?0?you?should?set?dz?to?0?as?well.
????dZ[Z?<=?0]?=?0
????assert?(dZ.shape?==?Z.shape)
????return?dZ
def?layer_sizes(X?Y):
????“““
????Arguments:
????X?--?input?dataset?of?shape?(input?size?number?of?examples)
????Y?--?labels?of?shape?(output?size?number?of?examples)
????Returns:
????n_x?--?the?size?of?the?input?layer
????n_h?--?the?size?of?the?hidden?layer
????n_y?--?the?size?of?the?output?layer
????“““
????n_x?=?X.shape[0]??#?size?of?input?layer?number?of?features??
????n_h?=?4
????n_y?=?Y.shape[0]??#?size?of?output?layer
????return?(n_x?n_h?n_y)
def?initialize_parameters(n_x?n_h?n_y):
????“““
????Argument:
????n_x?--?size?of?the?input?layer
????n_h?--?size?of?the?hidden?layer
????n_y?--?size?of?the?output?layer
????Returns:
????params?--?python?dictionary?containing?your?parameters:
????W1?--?weight?matrix?of?shape?(n_h?n_x)
????b1?--?bias?vector?of?shape?(n_h?1)
????W2?--?weight?matrix?of?shape?(n_y?n_h)
????b2?--?bias?vector?of?shape?(n_y?1)
????“““
????np.random.seed(2)??#?we?set?up?a?seed?so?that?your?output?matches?ours?although?the?initialization?is?random.
????W1?=?np.random.randn(n_h?n_x)?*?0.01
????b1?=?np.zeros((n_h?1))
????W2?=?np.random.r
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????11828??2018-08-29?08:47??pred_nn.py
?????文件???????12435??2018-04-28?00:36??housing.csv
評論
共有 條評論