Home>

We use deep learning to build a prediction model for road restoration period days.
We have established a neural network with six types of explanatory variables (the degree of road collapse, the height of road embankments, the number of collapsed lanes, etc.) and one type of objective variable (number of days until restoration).

In that case, the inner product calculation error message of the following matrix occurred.

Error message
ValueError Traceback (most recent call last)
<ipython-input-42-64c8b1648510>in<module>
      1 for i in range (epoch):
---->2 for j in range (0, x_train.shape [0],

 batch_size): w1, b1, w2, b2, w3, b3 = learn (x_train [j: j + batch_size],

 t_train [j: j + batch_size],

 w1, b1, w2, b2, w3, b3, lr)
      3 # Learning shape [00] checks the line size. Here,, 77. Therefore, create a sequence with the step size of the batch size sentence in the range of 0 to 77
<ipython-input-33-1c9a75ec0d92>in learn (x, t, w1, b1, w2, b2, w3, b3, lr)
     10 # Backpropagation (part for calculating the gradient and updating the weight)
     11 dy = identity_mean_squared_error_back (y, t)
--->12 dz2, dw3, db3 = affine_back (dy, z2, w3, b3)
     13 du2 = sigmoid_back (u2)
     14 dz1, dw2, db2 = affine_back (du2, z1, w2, b2)
<ipython-input-30-e9009ace5ac4>in affine_back (du, z, w, b)
      9 # affine transformation gradient
     10 def affine_back (du, z, w, b):
--->11 dz = np.dot (du, w.T)
     12 dw = np.dot (z.T, du)
     13 db = np.dot (np.ones (z.shape [0]). T, du)
ValueError: shapes (32,2) and (1,30) not aligned: 2 (dim 1)! = 1 (dim 0)
Applicable source code
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# affine converted
def affine (z, w, b):
return np.dot (z, w) + b
# affine transformation gradient
def affine_back (du, z, w, b):
dz = np.dot (du, w.T)
dw = np.dot (z.T, du)
db = np.dot (np.ones (z.shape [0]). T, du)
return dz, dw, db
def sigmoid (u):
return 1/(1 + np.exp (u))
#Activation function Sigmoid gradient, intermediate layer gradient
def sigmoid_back (u):
return (1 sigmoid (u)) * sigmoid (u)
def identity (u):
return u#Square sum error: regression problem
def squared_error (y, t):
return 0.5 * np.sum ((y t) ** 2)/y.shape [0]

# Error sum of squares error) + activation function identity function gradient
def identity_mean_s
quared_error_back (y, t):
return (y t)/y.shape [0]
#Learning part
def learn (x, t, w1, b1, w2, b2, w3, b3, lr):
#Forward propagation From input data to obtaining prediction data
u1 = affine (x, w1, b1)
z1 = sigmoid (u1)
u2 = affine (z1, w2, b2)
z2 = sigmoid (u2)
u3 = affine (z2, w3, b3)
y = identity (u3)
#The part that calculates the back propagation gradient and updates the weight
dy = identity_mean_squared_error_back (y, t)
dz2, dw3, db3 = affine_back (dy, z2, w3, b3)
du2 = sigmoid_back (u2)
dz1, dw2, db2 = affine_back (du2, z1, w2, b2)
du 1 = sigmoid_back (u1)
dx, dw1, db1 = affine_back (du1, x, w1, b1)
#Update weight and bias
w1 = w1 lr * dw1
b1 = b1 lr * db1
w2 = w2 lr * dw2
b2 = b2 lr * db2
w3 = w3 lr * dw3
b3 = b3 lr * db3
return w1, b1, w2, b2, w3, b3
def predict (x, w1, b1, w2, b2, w3, b3):
# Forward propagation
u1 = affine (x, w1, b1)
z1 = sigmoid (u1)
u2 = affine (z1, w2, b2)
z2 = sigmoid (u2)
u3 = affine (z2, w3, b3)
y = identity (u3)
return y
#Execution program
#Load earthquake data
df = pd.read_csv ('earthquake_B_traffic.csv')
# Create data Create data Separate explanatory variables/objective variables Separate explanatory variables/objective variables
df_x = df.drop ("regulatory period", axis = 1)
p = df.drop ("damage range", axis = 1)
q = p.drop ("embankment height", axis = 1)
r = q.drop ("landform", axis = 1)
s = r.drop ("banking structure", axis = 1)t = s.drop ("damage form", axis = 1)
u = t.drop ("traffic", axis = 1)
# Divide explanatory variables and objective variables into training data and test data respectively Divide explanatory variables and objective variables into training data and test data respectively
x_train, x_test, t_train, t_test = train_test_split (df_x, t, test_size = 0.3)
#Set number of nodes
d0 = x_train.shape [1]
d1 = 30 # number of nodes in the first layer
d2 = 30 # number of nodes in the second layer
d3 = 1 # output layer
# Initialization of weight 0.1 Random number of 0.1
np.random.seed (8)
w1 = np.random.rand (d0, d1) * 0.001
w2 = np.random.rand (d1, d2) * 0.001
w3 = np.random.rand (d2, d3) * 0.001
#The part where the weight is set to 1 to 0.001

#Bias initialization (
b1 = np.zeros (d1)
b2 = np.zeros (d2)
b3 = np.z
eros (d3)
#Learning rate
lr = 0.5
#Batch size
batch_size = 32
#Learning times
epoch = 1000
#Draw graph list
x = [0, epoch]
y = [0.100]
for i in range (epoch):
for j in range (0, x_train.shape [0],

 batch_size): w1, b1, w2, b2, w3, b3 = learn (x_train [j: j + batch_size],

 t_train [j: j + batch_size],

 w1, b1, w2, b2, w3, b3,
# Learning shape [00 checks the line size. Here, 77. Therefore, create a sequence with the step size of batch size sentence in the range of 0 to 77

By reducing the number of explanatory variables from 6 to 5, the program ran without error.
This refers to a model of character recognition using deep learning.

Supplemental information (FW/tool version etc.)

(32,2)
→ 32: Batch size
→ 2:?

(1,30)
→ 1: Output layer size
→ 30: Number of nodes in the second layer

I know

.

  • Answer # 1

    # Create data Create data Separate explanatory variables and objective variables Separate explanatory variables and objective variables
    df_x = df.drop ("regulatory period", axis = 1)
    p = df.drop ("damage range", axis = 1)
    q = p.drop ("embankment height", axis = 1)
    r = q.drop ("landform", axis = 1)
    s = r.drop ("banking structure", axis = 1)
    t = s.drop ("damage form", axis = 1)
    u = t.drop ("traffic", axis = 1)
    # Divide explanatory variables and objective variables into training data and test data respectively Divide explanatory variables and objective variables into training data and test data respectively
    x_train, x_test, t_train, t_test = train_test_split (df_x, t, test_size = 0.3)
    It was solved by changing "t" to "u" in the last line "(df_x, t, test_size = 0.3)" in the

    part.

    An error occurred when running a program without considering the last "traffic" line of the explanatory variable when the model was originally built with 5 explanatory variables and increased to 6 types. Had occurred.