## **pytorch DNN strategy**

```python
import torch
from torch.autograd import Variable  # 将多维矩阵放入Variable，可以反向传播
import torch.nn as nn  #参数扩展，计算权重
import torch.nn.functional as F
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score  #评价测试结果，所有分类正确的百分比
from torchvision import datasets
from torchvision import transforms
from tensorflow import keras
import sympy
from sympy import Matrix
import tushare as ts

%config INlineBackend.figure_format='svg' #  为了让数字更清晰的显示在notebook中
```

```python
torch.cuda.is_available()
```

    False

```python
data = ts.get_k_data('hs300',start = '2014-07-01',end = '2021-01-20')
data.set_index('date',inplace=True)
data
```

    本接口即将停止更新，请尽快使用Pro版接口：https://waditu.com/document/2

      open
      close
      high
      low
      volume
      code

      date

      2014-07-01
      2169.20
      2164.56
      2171.15
      2157.13
      53323100.0
      hs300

      2014-07-02
      2164.00
      2170.87
      2171.51
      2155.61
      59685501.0
      hs300

      2014-07-03
      2169.00
      2180.19
      2184.96
      2164.84
      63690690.0
      hs300

      2014-07-04
      2180.45
      2178.70
      2183.80
      2174.07
      52851106.0
      hs300

      2014-07-07
      2178.55
      2176.29
      2186.20
      2171.08
      50242334.0
      hs300

      ...
      ...
      ...
      ...
      ...
      ...
      ...

      2021-01-14
      5556.21
      5470.46
      5568.02
      5458.68
      243683380.0
      hs300

      2021-01-15
      5471.39
      5458.08
      5500.63
      5390.27
      238637435.0
      hs300

      2021-01-18
      5438.16
      5518.52
      5541.46
      5410.78
      207051869.0
      hs300

      2021-01-19
      5525.97
      5437.52
      5532.48
      5415.72
      211043425.0
      hs300

      2021-01-20
      5439.91
      5476.43
      5496.05
      5426.54
      170913260.0
      hs300

1601 rows × 6 columns

```python
data_cleaned = pd.DataFrame()
data_cleaned['Close0'] = np.log(data['close'])
data_cleaned['H_L'] = data['high']-data['low']
data_cleaned['C_O'] = data['close']-data['open']
data_cleaned['volume'] = np.log10(data['volume'])

data_cleaned['Close1'] = data_cleaned['Close0'].shift(1)
data_cleaned['Close2'] = data_cleaned['Close0'].shift(2)
data_cleaned['Close3'] = data_cleaned['Close0'].shift(3)
data_cleaned['Close4'] = data_cleaned['Close0'].shift(4)
data_cleaned['Close5'] = data_cleaned['Close0'].shift(5)

data_cleaned['H_L1'] = data_cleaned['H_L'].shift(1)
data_cleaned['H_L2'] = data_cleaned['H_L'].shift(2)
data_cleaned['H_L3'] = data_cleaned['H_L'].shift(3)
data_cleaned['H_L4'] = data_cleaned['H_L'].shift(4)
data_cleaned['H_L5'] = data_cleaned['H_L'].shift(5)

data_cleaned['C_O1'] = data_cleaned['C_O'].shift(1)
data_cleaned['C_O2'] = data_cleaned['C_O'].shift(2)
data_cleaned['C_O3'] = data_cleaned['C_O'].shift(3)
data_cleaned['C_O4'] = data_cleaned['C_O'].shift(4)
data_cleaned['C_O5'] = data_cleaned['C_O'].shift(5)

data_cleaned['volume1'] = data_cleaned['volume'].shift(1)
data_cleaned['volume2'] = data_cleaned['volume'].shift(2)
data_cleaned['volume3'] = data_cleaned['volume'].shift(3)
data_cleaned['volume4'] = data_cleaned['volume'].shift(4)
data_cleaned['volume5'] = data_cleaned['volume'].shift(5)

data_cleaned['Close0_shift(-1)'] = data_cleaned['Close0'].shift(-1)
data_cleaned['label'] =0.5 + 0.5*np.sign(data_cleaned['Close0_shift(-1)']-data_cleaned['Close0'])
data_cleaned.head(23)
```

      Close0
      H_L
      C_O
      volume
      Close1
      Close2
      Close3
      Close4
      Close5
      H_L1
      ...
      C_O3
      C_O4
      C_O5
      volume1
      volume2
      volume3
      volume4
      volume5
      Close0_shift(-1)
      label

      date

      2014-07-01
      7.679972
      14.02
      -4.64
      7.726915
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      ...
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      7.682883
      1.0

      2014-07-02
      7.682883
      15.90
      6.87
      7.775869
      7.679972
      NaN
      NaN
      NaN
      NaN
      14.02
      ...
      NaN
      NaN
      NaN
      7.726915
      NaN
      NaN
      NaN
      NaN
      7.687167
      1.0

      2014-07-03
      7.687167
      20.12
      11.19
      7.804076
      7.682883
      7.679972
      NaN
      NaN
      NaN
      15.90
      ...
      NaN
      NaN
      NaN
      7.775869
      7.726915
      NaN
      NaN
      NaN
      7.686484
      0.0

      2014-07-04
      7.686484
      9.73
      -1.75
      7.723054
      7.687167
      7.682883
      7.679972
      NaN
      NaN
      20.12
      ...
      -4.64
      NaN
      NaN
      7.804076
      7.775869
      7.726915
      NaN
      NaN
      7.685377
      0.0

      2014-07-07
      7.685377
      15.12
      -2.26
      7.701070
      7.686484
      7.687167
      7.682883
      7.679972
      NaN
      9.73
      ...
      6.87
      -4.64
      NaN
      7.723054
      7.804076
      7.775869
      7.726915
      NaN
      7.687296
      1.0

      2014-07-08
      7.687296
      17.42
      5.64
      7.705381
      7.685377
      7.686484
      7.687167
      7.682883
      7.679972
      15.12
      ...
      11.19
      6.87
      -4.64
      7.701070
      7.723054
      7.804076
      7.775869
      7.726915
      7.672623
      0.0

      2014-07-09
      7.672623
      30.25
      -29.43
      7.792546
      7.687296
      7.685377
      7.686484
      7.687167
      7.682883
      17.42
      ...
      -1.75
      11.19
      6.87
      7.705381
      7.701070
      7.723054
      7.804076
      7.775869
      7.669892
      0.0

      2014-07-10
      7.669892
      12.25
      -3.75
      7.746109
      7.672623
      7.687296
      7.685377
      7.686484
      7.687167
      30.25
      ...
      -2.26
      -1.75
      11.19
      7.792546
      7.705381
      7.701070
      7.723054
      7.804076
      7.672297
      1.0

      2014-07-11
      7.672297
      19.95
      11.69
      7.787986
      7.669892
      7.672623
      7.687296
      7.685377
      7.686484
      12.25
      ...
      5.64
      -2.26
      -1.75
      7.746109
      7.792546
      7.705381
      7.701070
      7.723054
      7.683293
      1.0

      2014-07-14
      7.683293
      26.62
      22.22
      7.820588
      7.672297
      7.669892
      7.672623
      7.687296
      7.685377
      19.95
      ...
      -29.43
      5.64
      -2.26
      7.787986
      7.746109
      7.792546
      7.705381
      7.701070
      7.684775
      1.0

      2014-07-15
      7.684775
      11.06
      4.89
      7.856219
      7.683293
      7.672297
      7.669892
      7.672623
      7.687296
      26.62
      ...
      -3.75
      -29.43
      5.64
      7.820588
      7.787986
      7.746109
      7.792546
      7.705381
      7.682883
      0.0

      2014-07-16
      7.682883
      16.48
      -1.95
      7.911478
      7.684775
      7.683293
      7.672297
      7.669892
      7.672623
      11.06
      ...
      11.69
      -3.75
      -29.43
      7.856219
      7.820588
      7.787986
      7.746109
      7.792546
      7.676506
      0.0

      2014-07-17
      7.676506
      19.85
      -9.76
      7.774178
      7.682883
      7.684775
      7.683293
      7.672297
      7.669892
      16.48
      ...
      22.22
      11.69
      -3.75
      7.911478
      7.856219
      7.820588
      7.787986
      7.746109
      7.679778
      1.0

      2014-07-18
      7.679778
      30.87
      16.48
      7.816064
      7.676506
      7.682883
      7.684775
      7.683293
      7.672297
      19.85
      ...
      4.89
      22.22
      11.69
      7.774178
      7.911478
      7.856219
      7.820588
      7.787986
      7.680771
      1.0

      2014-07-21
      7.680771
      12.98
      2.07
      7.733777
      7.679778
      7.676506
      7.682883
      7.684775
      7.683293
      30.87
      ...
      -1.95
      4.89
      22.22
      7.816064
      7.774178
      7.911478
      7.856219
      7.820588
      7.692889
      1.0

      2014-07-22
      7.692889
      35.35
      30.26
      7.895105
      7.680771
      7.679778
      7.676506
      7.682883
      7.684775
      12.98
      ...
      -9.76
      -1.95
      4.89
      7.733777
      7.816064
      7.774178
      7.911478
      7.856219
      7.695226
      1.0

      2014-07-23
      7.695226
      19.73
      6.44
      7.925384
      7.692889
      7.680771
      7.679778
      7.676506
      7.682883
      35.35
      ...
      16.48
      -9.76
      -1.95
      7.895105
      7.733777
      7.816064
      7.774178
      7.911478
      7.712895
      1.0

      2014-07-24
      7.712895
      39.98
      37.18
      8.077134
      7.695226
      7.692889
      7.680771
      7.679778
      7.676506
      19.73
      ...
      2.07
      16.48
      -9.76
      7.925384
      7.895105
      7.733777
      7.816064
      7.774178
      7.723319
      1.0

      2014-07-25
      7.723319
      21.82
      17.38
      7.995516
      7.712895
      7.695226
      7.692889
      7.680771
      7.679778
      39.98
      ...
      30.26
      2.07
      16.48
      8.077134
      7.925384
      7.895105
      7.733777
      7.816064
      7.751002
      1.0

      2014-07-28
      7.751002
      59.09
      51.52
      8.254957
      7.723319
      7.712895
      7.695226
      7.692889
      7.680771
      21.82
      ...
      6.44
      30.26
      2.07
      7.995516
      8.077134
      7.925384
      7.895105
      7.733777
      7.754211
      1.0

      2014-07-29
      7.754211
      26.55
      5.33
      8.127978
      7.751002
      7.723319
      7.712895
      7.695226
      7.692889
      59.09
      ...
      37.18
      6.44
      30.26
      8.254957
      7.995516
      8.077134
      7.925384
      7.895105
      7.750188
      0.0

      2014-07-30
      7.750188
      22.98
      -2.68
      8.089986
      7.754211
      7.751002
      7.723319
      7.712895
      7.695226
      26.55
      ...
      17.38
      37.18
      6.44
      8.127978
      8.254957
      7.995516
      8.077134
      7.925384
      7.762277
      1.0

      2014-07-31
      7.762277
      33.97
      28.28
      8.045275
      7.750188
      7.754211
      7.751002
      7.723319
      7.712895
      22.98
      ...
      51.52
      17.38
      37.18
      8.089986
      8.127978
      8.254957
      7.995516
      8.077134
      7.753366
      0.0

23 rows × 26 columns

```python
# 去掉多余的数据和空值
data_cleaned.dropna(inplace=True)
data_cleaned.drop(['Close0_shift(-1)'],axis=1,inplace=True)
data_cleaned
```

      Close0
      H_L
      C_O
      volume
      Close1
      Close2
      Close3
      Close4
      Close5
      H_L1
      ...
      C_O2
      C_O3
      C_O4
      C_O5
      volume1
      volume2
      volume3
      volume4
      volume5
      label

      date

      2014-07-08
      7.687296
      17.42
      5.64
      7.705381
      7.685377
      7.686484
      7.687167
      7.682883
      7.679972
      15.12
      ...
      -1.75
      11.19
      6.87
      -4.64
      7.701070
      7.723054
      7.804076
      7.775869
      7.726915
      0.0

      2014-07-09
      7.672623
      30.25
      -29.43
      7.792546
      7.687296
      7.685377
      7.686484
      7.687167
      7.682883
      17.42
      ...
      -2.26
      -1.75
      11.19
      6.87
      7.705381
      7.701070
      7.723054
      7.804076
      7.775869
      0.0

      2014-07-10
      7.669892
      12.25
      -3.75
      7.746109
      7.672623
      7.687296
      7.685377
      7.686484
      7.687167
      30.25
      ...
      5.64
      -2.26
      -1.75
      11.19
      7.792546
      7.705381
      7.701070
      7.723054
      7.804076
      1.0

      2014-07-11
      7.672297
      19.95
      11.69
      7.787986
      7.669892
      7.672623
      7.687296
      7.685377
      7.686484
      12.25
      ...
      -29.43
      5.64
      -2.26
      -1.75
      7.746109
      7.792546
      7.705381
      7.701070
      7.723054
      1.0

      2014-07-14
      7.683293
      26.62
      22.22
      7.820588
      7.672297
      7.669892
      7.672623
      7.687296
      7.685377
      19.95
      ...
      -3.75
      -29.43
      5.64
      -2.26
      7.787986
      7.746109
      7.792546
      7.705381
      7.701070
      1.0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      2021-01-13
      8.626580
      109.58
      -31.29
      8.417588
      8.629870
      8.601748
      8.611672
      8.614984
      8.597421
      178.72
      ...
      -63.08
      -30.86
      85.27
      31.16
      8.350675
      8.365181
      8.310321
      8.342170
      8.287294
      0.0

      2021-01-14
      8.607118
      109.34
      -85.75
      8.386826
      8.626580
      8.629870
      8.601748
      8.611672
      8.614984
      109.58
      ...
      176.85
      -63.08
      -30.86
      85.27
      8.417588
      8.350675
      8.365181
      8.310321
      8.342170
      0.0

      2021-01-15
      8.604852
      110.36
      -13.31
      8.377739
      8.607118
      8.626580
      8.629870
      8.601748
      8.611672
      109.34
      ...
      -31.29
      176.85
      -63.08
      -30.86
      8.386826
      8.417588
      8.350675
      8.365181
      8.310321
      1.0

      2021-01-18
      8.615865
      130.68
      80.36
      8.316079
      8.604852
      8.607118
      8.626580
      8.629870
      8.601748
      110.36
      ...
      -85.75
      -31.29
      176.85
      -63.08
      8.377739
      8.386826
      8.417588
      8.350675
      8.365181
      0.0

      2021-01-19
      8.601078
      116.76
      -88.45
      8.324372
      8.615865
      8.604852
      8.607118
      8.626580
      8.629870
      130.68
      ...
      -13.31
      -85.75
      -31.29
      176.85
      8.316079
      8.377739
      8.386826
      8.417588
      8.350675
      1.0

1595 rows × 25 columns

```python
# 将数据分成训练集和测试集
traning_set = data_cleaned.iloc[:999,:]
test_set = data_cleaned.iloc[999:,:]
```

```python
traning_set
```

      Close0
      H_L
      C_O
      volume
      Close1
      Close2
      Close3
      Close4
      Close5
      H_L1
      ...
      C_O2
      C_O3
      C_O4
      C_O5
      volume1
      volume2
      volume3
      volume4
      volume5
      label

      date

      2014-07-08
      7.687296
      17.42
      5.64
      7.705381
      7.685377
      7.686484
      7.687167
      7.682883
      7.679972
      15.12
      ...
      -1.75
      11.19
      6.87
      -4.64
      7.701070
      7.723054
      7.804076
      7.775869
      7.726915
      0.0

      2014-07-09
      7.672623
      30.25
      -29.43
      7.792546
      7.687296
      7.685377
      7.686484
      7.687167
      7.682883
      17.42
      ...
      -2.26
      -1.75
      11.19
      6.87
      7.705381
      7.701070
      7.723054
      7.804076
      7.775869
      0.0

      2014-07-10
      7.669892
      12.25
      -3.75
      7.746109
      7.672623
      7.687296
      7.685377
      7.686484
      7.687167
      30.25
      ...
      5.64
      -2.26
      -1.75
      11.19
      7.792546
      7.705381
      7.701070
      7.723054
      7.804076
      1.0

      2014-07-11
      7.672297
      19.95
      11.69
      7.787986
      7.669892
      7.672623
      7.687296
      7.685377
      7.686484
      12.25
      ...
      -29.43
      5.64
      -2.26
      -1.75
      7.746109
      7.792546
      7.705381
      7.701070
      7.723054
      1.0

      2014-07-14
      7.683293
      26.62
      22.22
      7.820588
      7.672297
      7.669892
      7.672623
      7.687296
      7.685377
      19.95
      ...
      -3.75
      -29.43
      5.64
      -2.26
      7.787986
      7.746109
      7.792546
      7.705381
      7.701070
      1.0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      2018-08-01
      8.145373
      96.83
      -82.64
      7.955922
      8.165551
      8.164818
      8.166566
      8.170822
      8.182489
      35.00
      ...
      -5.77
      -12.54
      -47.07
      -11.76
      7.832386
      7.914433
      7.846202
      7.955926
      7.985689
      0.0

      2018-08-02
      8.122953
      112.25
      -63.39
      8.005647
      8.145373
      8.165551
      8.164818
      8.166566
      8.170822
      96.83
      ...
      7.19
      -5.77
      -12.54
      -47.07
      7.955922
      7.832386
      7.914433
      7.846202
      7.955926
      0.0

      2018-08-03
      8.106297
      66.14
      -51.38
      7.850014
      8.122953
      8.145373
      8.165551
      8.164818
      8.166566
      112.25
      ...
      -82.64
      7.19
      -5.77
      -12.54
      8.005647
      7.955922
      7.832386
      7.914433
      7.846202
      0.0

      2018-08-06
      8.093545
      86.56
      -39.55
      7.891956
      8.106297
      8.122953
      8.145373
      8.165551
      8.164818
      66.14
      ...
      -63.39
      -82.64
      7.19
      -5.77
      7.850014
      8.005647
      7.955922
      7.832386
      7.914433
      1.0

      2018-08-07
      8.122333
      105.46
      83.89
      7.962767
      8.093545
      8.106297
      8.122953
      8.145373
      8.165551
      86.56
      ...
      -51.38
      -63.39
      -82.64
      7.19
      7.891956
      7.850014
      8.005647
      7.955922
      7.832386
      0.0

999 rows × 25 columns

```python
# 查看维度
traning_set.shape
```

    (999, 25)

## **Traning DNN Model**

```python
class DeepNeuralNetworkMdoel(nn.Module):
    # constructor of the class
    def __init__(self):
        super(DeepNeuralNetworkMdoel,self).__init__()
        # Fully connected Layer1
        self.FC_layer1 = nn.Linear(24,48)

        # Fully connected Layer2
        self.FC_layer2 = nn.Linear(48,36)

        # Fully connected Layer3
        self.FC_layer3 = nn.Linear(36,24)

        # Fully connected Layer4
        self.FC_layer4 = nn.Linear(24,12)

        # Fully connected Layer5
        self.FC_layer5 = nn.Linear(12,2)

    # Forward propagation function
    def forward(self,input_data):  # dim of input_data N * 24
        z1_= self.FC_layer1(input_data)
        z1 = torch.sigmoid(z1_)

        z2_= self.FC_layer2(z1)
        z2 = torch.sigmoid(z2_)

        z3_= self.FC_layer3(z2)
        z3 = torch.sigmoid(z3_)

        z4_= self.FC_layer4(z3)
        z4 = torch.sigmoid(z4_)

        z5_= self.FC_layer5(z4)   # 最后一层不加激活函数,后面调用entrypyloss函数

        return z5_
```

```python
x = traning_set.iloc[:,0:24]
y = traning_set.iloc[:,24]
x = np.array(x)
y = np.array(y)
x,y
```

    (array([[  7.68729573,  17.42      ,   5.64      , ...,   7.80407595,
               7.77586884,   7.72691539],
            [  7.67262294,  30.25      , -29.43      , ...,   7.72305408,
               7.80407595,   7.77586884],
            [  7.669892  ,  12.25      ,  -3.75      , ...,   7.70106981,
               7.72305408,   7.80407595],
            ...,
            [  8.10629736,  66.14      , -51.38      , ...,   7.83238638,
               7.91443305,   7.84620163],
            [  8.09354476,  86.56      , -39.55      , ...,   7.95592151,
               7.83238638,   7.91443305],
            [  8.12233266, 105.46      ,  83.89      , ...,   8.00564686,
               7.95592151,   7.83238638]]),
     array([0., 0., 1., 1., 1., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1.,
            0., 1., 0., 0., 0., 1., 1., 0., 1., 0., 1., 1., 1., 0., 0., 1., 0.,
            0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0., 0., 1., 0., 0., 1., 1.,
            1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 0., 0., 0., 1., 0., 0., 1.,
            0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 0., 1., 0., 1., 0., 1.,
            0., 1., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
            1., 0., 1., 0., 1., 1., 1., 1., 0., 1., 1., 0., 0., 1., 1., 1., 1.,
            1., 1., 0., 1., 0., 0., 0., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1.,
            0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 0.,
            1., 1., 1., 0., 1., 0., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 1.,
            1., 1., 0., 1., 1., 1., 0., 1., 1., 1., 1., 1., 0., 1., 1., 1., 0.,
            1., 1., 0., 1., 1., 1., 0., 1., 0., 1., 0., 1., 0., 0., 0., 1., 1.,
            1., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 0., 0., 1., 1., 1., 0.,
            1., 1., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 1., 1., 0., 0., 0.,
            1., 0., 0., 0., 1., 0., 0., 1., 1., 1., 0., 0., 1., 1., 1., 1., 0.,
            1., 0., 0., 0., 1., 0., 1., 1., 1., 0., 0., 1., 1., 0., 0., 1., 0.,
            1., 0., 1., 0., 0., 0., 0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 0.,
            0., 0., 0., 1., 0., 1., 1., 1., 0., 1., 0., 1., 0., 1., 1., 1., 1.,
            0., 0., 1., 1., 1., 1., 0., 1., 1., 1., 1., 0., 1., 1., 0., 0., 1.,
            1., 1., 1., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 1., 1., 0., 0.,
            1., 1., 1., 1., 0., 1., 0., 1., 0., 0., 1., 0., 0., 1., 1., 1., 1.,
            0., 0., 1., 0., 1., 1., 0., 0., 1., 1., 0., 1., 0., 1., 0., 1., 0.,
            1., 1., 0., 0., 1., 1., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., 1.,
            1., 0., 0., 1., 0., 1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 0.,
            1., 1., 1., 1., 1., 1., 1., 0., 1., 0., 1., 0., 0., 1., 1., 1., 1.,
            0., 0., 0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 1., 0., 1., 0., 0.,
            0., 1., 0., 1., 0., 0., 1., 1., 1., 0., 1., 0., 0., 0., 1., 1., 0.,
            0., 1., 0., 1., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 0., 1., 1.,
            0., 1., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 1., 1., 1.,
            0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 1., 0., 0., 1., 1., 1., 1.,
            1., 1., 0., 0., 1., 1., 0., 0., 0., 1., 0., 1., 0., 0., 0., 1., 1.,
            1., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 0.,
            1., 0., 1., 1., 1., 1., 0., 1., 1., 0., 1., 0., 1., 1., 1., 0., 0.,
            0., 0., 0., 1., 0., 1., 0., 1., 1., 0., 1., 1., 1., 0., 0., 1., 0.,
            1., 1., 1., 1., 1., 1., 1., 0., 1., 0., 0., 0., 1., 0., 1., 0., 0.,
            0., 0., 1., 0., 0., 1., 0., 0., 1., 0., 0., 0., 1., 1., 1., 0., 0.,
            1., 0., 0., 0., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 0., 1., 0.,
            1., 1., 1., 1., 0., 0., 1., 0., 1., 1., 1., 0., 1., 0., 1., 1., 0.,
            0., 1., 1., 0., 0., 1., 1., 0., 1., 1., 0., 1., 1., 0., 1., 1., 0.,
            0., 0., 0., 1., 1., 1., 1., 0., 1., 0., 1., 0., 0., 0., 0., 1., 1.,
            0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 0.,
            0., 1., 1., 1., 0., 1., 0., 1., 1., 0., 0., 1., 1., 1., 1., 0., 1.,
            0., 0., 0., 1., 0., 1., 1., 1., 1., 1., 0., 1., 0., 0., 0., 1., 1.,
            0., 0., 1., 0., 1., 1., 0., 1., 1., 1., 0., 1., 0., 0., 1., 1., 1.,
            1., 0., 0., 0., 1., 1., 0., 0., 0., 1., 1., 0., 1., 1., 1., 1., 1.,
            0., 1., 1., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0., 1., 1., 0., 1.,
            1., 0., 1., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0.,
            1., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 1., 0., 1., 1., 0., 1.,
            1., 1., 0., 0., 1., 1., 1., 1., 1., 0., 1., 0., 1., 0., 0., 0., 1.,
            1., 0., 0., 1., 1., 0., 1., 0., 0., 1., 1., 0., 1., 0., 0., 1., 0.,
            1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1.,
            1., 1., 0., 1., 0., 0., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 1.,
            1., 1., 1., 0., 0., 1., 0., 1., 1., 0., 1., 1., 1., 0., 0., 1., 0.,
            1., 1., 0., 0., 0., 0., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 0.,
            0., 0., 0., 1., 1., 0., 1., 1., 0., 0., 1., 1., 1., 0., 1., 1., 0.,
            1., 0., 1., 1., 0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 0., 1., 0.,
            1., 1., 0., 0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0.,
            0., 1., 0., 1., 0., 0., 1., 1., 1., 0., 1., 1., 0., 0., 0., 0., 1.,
            1., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0.]))

```python
x = Variable(torch.FloatTensor(x))  # x为浮点类型的数据，所以用FloatTensor
y = Variable(torch.LongTensor(y)) # y需要转为int型数据
x.shape,y.shape
```

    (torch.Size([999, 24]), torch.Size([999]))

```python
alpha = 1.0
DNN_Model = DeepNeuralNetworkMdoel()
optimizer = torch.optim.SGD(DNN_Model.parameters(),lr = alpha)
loss_function = nn.CrossEntropyLoss()

# 动态调整学习率
def adjust_learning_rate(optimizer,epoch):
    if epoch <=100:
        lr = alpha
    elif epoch > 100:
        lr = alpha / (1 + 0.0008 * (epoch-100))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

```

```python
Iter_times = 10000000
loss_list = []
for i in range(Iter_times):
    outputs = DNN_Model.forward(x)  # 前向传播
    loss = loss_function(outputs,y) # 计算loss
    loss.backward() # 反向传播
    optimizer.step() # 更新参数
    optimizer.zero_grad() # 梯度清零

    if (i+1) % 500 == 0:
        print(i+1,'iterations have been completed!')
        print('     --> Now loss = ',loss)
        print('===========================')

    adjust_learning_rate(optimizer,i)
    loss_list.append(loss)
    if loss < 0.01:
        break
```

    500 iterations have been completed!
         --> Now loss =  tensor(0.6952, grad_fn=<NllLossBackward>)
    ===========================
    1000 iterations have been completed!
         --> Now loss =  tensor(0.5488, grad_fn=<NllLossBackward>)
    ===========================
    1500 iterations have been completed!
         --> Now loss =  tensor(0.3822, grad_fn=<NllLossBackward>)
    ===========================
    2000 iterations have been completed!
         --> Now loss =  tensor(0.3453, grad_fn=<NllLossBackward>)
    ===========================
    2500 iterations have been completed!
         --> Now loss =  tensor(0.1939, grad_fn=<NllLossBackward>)
    ===========================
    3000 iterations have been completed!
         --> Now loss =  tensor(0.1741, grad_fn=<NllLossBackward>)
    ===========================
    3500 iterations have been completed!
         --> Now loss =  tensor(0.1544, grad_fn=<NllLossBackward>)
    ===========================
    4000 iterations have been completed!
         --> Now loss =  tensor(0.1398, grad_fn=<NllLossBackward>)
    ===========================
    4500 iterations have been completed!
         --> Now loss =  tensor(0.1272, grad_fn=<NllLossBackward>)
    ===========================
    5000 iterations have been completed!
         --> Now loss =  tensor(0.1177, grad_fn=<NllLossBackward>)
    ===========================
    5500 iterations have been completed!
         --> Now loss =  tensor(0.1091, grad_fn=<NllLossBackward>)
    ===========================
    6000 iterations have been completed!
         --> Now loss =  tensor(0.1006, grad_fn=<NllLossBackward>)
    ===========================
    6500 iterations have been completed!
         --> Now loss =  tensor(0.0935, grad_fn=<NllLossBackward>)
    ===========================
    7000 iterations have been completed!
         --> Now loss =  tensor(0.0869, grad_fn=<NllLossBackward>)
    ===========================
    7500 iterations have been completed!
         --> Now loss =  tensor(0.0791, grad_fn=<NllLossBackward>)
    ===========================
    8000 iterations have been completed!
         --> Now loss =  tensor(0.0712, grad_fn=<NllLossBackward>)
    ===========================
    8500 iterations have been completed!
         --> Now loss =  tensor(0.0667, grad_fn=<NllLossBackward>)
    ===========================
    9000 iterations have been completed!
         --> Now loss =  tensor(0.0610, grad_fn=<NllLossBackward>)
    ===========================
    9500 iterations have been completed!
         --> Now loss =  tensor(0.0568, grad_fn=<NllLossBackward>)
    ===========================
    10000 iterations have been completed!
         --> Now loss =  tensor(0.0517, grad_fn=<NllLossBackward>)
    ===========================
    10500 iterations have been completed!
         --> Now loss =  tensor(0.0485, grad_fn=<NllLossBackward>)
    ===========================
    11000 iterations have been completed!
         --> Now loss =  tensor(0.0451, grad_fn=<NllLossBackward>)
    ===========================
    11500 iterations have been completed!
         --> Now loss =  tensor(0.0427, grad_fn=<NllLossBackward>)
    ===========================
    12000 iterations have been completed!
         --> Now loss =  tensor(0.0401, grad_fn=<NllLossBackward>)
    ===========================
    12500 iterations have been completed!
         --> Now loss =  tensor(0.0381, grad_fn=<NllLossBackward>)
    ===========================
    13000 iterations have been completed!
         --> Now loss =  tensor(0.0365, grad_fn=<NllLossBackward>)
    ===========================
    13500 iterations have been completed!
         --> Now loss =  tensor(0.0347, grad_fn=<NllLossBackward>)
    ===========================
    14000 iterations have been completed!
         --> Now loss =  tensor(0.0332, grad_fn=<NllLossBackward>)
    ===========================
    14500 iterations have been completed!
         --> Now loss =  tensor(0.0319, grad_fn=<NllLossBackward>)
    ===========================
    15000 iterations have been completed!
         --> Now loss =  tensor(0.0306, grad_fn=<NllLossBackward>)
    ===========================
    15500 iterations have been completed!
         --> Now loss =  tensor(0.0295, grad_fn=<NllLossBackward>)
    ===========================
    16000 iterations have been completed!
         --> Now loss =  tensor(0.0284, grad_fn=<NllLossBackward>)
    ===========================
    16500 iterations have been completed!
         --> Now loss =  tensor(0.0275, grad_fn=<NllLossBackward>)
    ===========================
    17000 iterations have been completed!
         --> Now loss =  tensor(0.0267, grad_fn=<NllLossBackward>)
    ===========================
    17500 iterations have been completed!
         --> Now loss =  tensor(0.0260, grad_fn=<NllLossBackward>)
    ===========================
    18000 iterations have been completed!
         --> Now loss =  tensor(0.0254, grad_fn=<NllLossBackward>)
    ===========================
    18500 iterations have been completed!
         --> Now loss =  tensor(0.0249, grad_fn=<NllLossBackward>)
    ===========================
    19000 iterations have been completed!
         --> Now loss =  tensor(0.0245, grad_fn=<NllLossBackward>)
    ===========================
    19500 iterations have been completed!
         --> Now loss =  tensor(0.0241, grad_fn=<NllLossBackward>)
    ===========================
    20000 iterations have been completed!
         --> Now loss =  tensor(0.0237, grad_fn=<NllLossBackward>)
    ===========================
    20500 iterations have been completed!
         --> Now loss =  tensor(0.0234, grad_fn=<NllLossBackward>)
    ===========================
    21000 iterations have been completed!
         --> Now loss =  tensor(0.0231, grad_fn=<NllLossBackward>)
    ===========================
    21500 iterations have been completed!
         --> Now loss =  tensor(0.0228, grad_fn=<NllLossBackward>)
    ===========================
    22000 iterations have been completed!
         --> Now loss =  tensor(0.0226, grad_fn=<NllLossBackward>)
    ===========================
    22500 iterations have been completed!
         --> Now loss =  tensor(0.0223, grad_fn=<NllLossBackward>)
    ===========================
    23000 iterations have been completed!
         --> Now loss =  tensor(0.0221, grad_fn=<NllLossBackward>)
    ===========================
    23500 iterations have been completed!
         --> Now loss =  tensor(0.0219, grad_fn=<NllLossBackward>)
    ===========================
    24000 iterations have been completed!
         --> Now loss =  tensor(0.0217, grad_fn=<NllLossBackward>)
    ===========================
    24500 iterations have been completed!
         --> Now loss =  tensor(0.0215, grad_fn=<NllLossBackward>)
    ===========================
    25000 iterations have been completed!
         --> Now loss =  tensor(0.0214, grad_fn=<NllLossBackward>)
    ===========================
    25500 iterations have been completed!
         --> Now loss =  tensor(0.0212, grad_fn=<NllLossBackward>)
    ===========================
    26000 iterations have been completed!
         --> Now loss =  tensor(0.0210, grad_fn=<NllLossBackward>)
    ===========================
    26500 iterations have been completed!
         --> Now loss =  tensor(0.0209, grad_fn=<NllLossBackward>)
    ===========================
    27000 iterations have been completed!
         --> Now loss =  tensor(0.0207, grad_fn=<NllLossBackward>)
    ===========================
    27500 iterations have been completed!
         --> Now loss =  tensor(0.0206, grad_fn=<NllLossBackward>)
    ===========================
    28000 iterations have been completed!
         --> Now loss =  tensor(0.0199, grad_fn=<NllLossBackward>)
    ===========================
    28500 iterations have been completed!
         --> Now loss =  tensor(0.0196, grad_fn=<NllLossBackward>)
    ===========================
    29000 iterations have been completed!
         --> Now loss =  tensor(0.0195, grad_fn=<NllLossBackward>)
    ===========================
    29500 iterations have been completed!
         --> Now loss =  tensor(0.0193, grad_fn=<NllLossBackward>)
    ===========================
    30000 iterations have been completed!
         --> Now loss =  tensor(0.0191, grad_fn=<NllLossBackward>)
    ===========================
    30500 iterations have been completed!
         --> Now loss =  tensor(0.0190, grad_fn=<NllLossBackward>)
    ===========================
    31000 iterations have been completed!
         --> Now loss =  tensor(0.0188, grad_fn=<NllLossBackward>)
    ===========================
    31500 iterations have been completed!
         --> Now loss =  tensor(0.0187, grad_fn=<NllLossBackward>)
    ===========================
    32000 iterations have been completed!
         --> Now loss =  tensor(0.0185, grad_fn=<NllLossBackward>)
    ===========================
    32500 iterations have been completed!
         --> Now loss =  tensor(0.0184, grad_fn=<NllLossBackward>)
    ===========================
    33000 iterations have been completed!
         --> Now loss =  tensor(0.0182, grad_fn=<NllLossBackward>)
    ===========================
    33500 iterations have been completed!
         --> Now loss =  tensor(0.0180, grad_fn=<NllLossBackward>)
    ===========================
    34000 iterations have been completed!
         --> Now loss =  tensor(0.0179, grad_fn=<NllLossBackward>)
    ===========================
    34500 iterations have been completed!
         --> Now loss =  tensor(0.0177, grad_fn=<NllLossBackward>)
    ===========================
    35000 iterations have been completed!
         --> Now loss =  tensor(0.0174, grad_fn=<NllLossBackward>)
    ===========================
    35500 iterations have been completed!
         --> Now loss =  tensor(0.0172, grad_fn=<NllLossBackward>)
    ===========================
    36000 iterations have been completed!
         --> Now loss =  tensor(0.0169, grad_fn=<NllLossBackward>)
    ===========================
    36500 iterations have been completed!
         --> Now loss =  tensor(0.0165, grad_fn=<NllLossBackward>)
    ===========================
    37000 iterations have been completed!
         --> Now loss =  tensor(0.0161, grad_fn=<NllLossBackward>)
    ===========================
    37500 iterations have been completed!
         --> Now loss =  tensor(0.0157, grad_fn=<NllLossBackward>)
    ===========================
    38000 iterations have been completed!
         --> Now loss =  tensor(0.0152, grad_fn=<NllLossBackward>)
    ===========================
    38500 iterations have been completed!
         --> Now loss =  tensor(0.0147, grad_fn=<NllLossBackward>)
    ===========================
    39000 iterations have been completed!
         --> Now loss =  tensor(0.0143, grad_fn=<NllLossBackward>)
    ===========================
    39500 iterations have been completed!
         --> Now loss =  tensor(0.0140, grad_fn=<NllLossBackward>)
    ===========================
    40000 iterations have been completed!
         --> Now loss =  tensor(0.0137, grad_fn=<NllLossBackward>)
    ===========================
    40500 iterations have been completed!
         --> Now loss =  tensor(0.0135, grad_fn=<NllLossBackward>)
    ===========================
    41000 iterations have been completed!
         --> Now loss =  tensor(0.0133, grad_fn=<NllLossBackward>)
    ===========================
    41500 iterations have been completed!
         --> Now loss =  tensor(0.0131, grad_fn=<NllLossBackward>)
    ===========================
    42000 iterations have been completed!
         --> Now loss =  tensor(0.0129, grad_fn=<NllLossBackward>)
    ===========================
    42500 iterations have been completed!
         --> Now loss =  tensor(0.0128, grad_fn=<NllLossBackward>)
    ===========================
    43000 iterations have been completed!
         --> Now loss =  tensor(0.0126, grad_fn=<NllLossBackward>)
    ===========================
    43500 iterations have been completed!
         --> Now loss =  tensor(0.0125, grad_fn=<NllLossBackward>)
    ===========================
    44000 iterations have been completed!
         --> Now loss =  tensor(0.0124, grad_fn=<NllLossBackward>)
    ===========================
    44500 iterations have been completed!
         --> Now loss =  tensor(0.0123, grad_fn=<NllLossBackward>)
    ===========================
    45000 iterations have been completed!
         --> Now loss =  tensor(0.0123, grad_fn=<NllLossBackward>)
    ===========================
    45500 iterations have been completed!
         --> Now loss =  tensor(0.0122, grad_fn=<NllLossBackward>)
    ===========================
    46000 iterations have been completed!
         --> Now loss =  tensor(0.0121, grad_fn=<NllLossBackward>)
    ===========================
    46500 iterations have been completed!
         --> Now loss =  tensor(0.0120, grad_fn=<NllLossBackward>)
    ===========================
    47000 iterations have been completed!
         --> Now loss =  tensor(0.0120, grad_fn=<NllLossBackward>)
    ===========================
    47500 iterations have been completed!
         --> Now loss =  tensor(0.0119, grad_fn=<NllLossBackward>)
    ===========================
    48000 iterations have been completed!
         --> Now loss =  tensor(0.0119, grad_fn=<NllLossBackward>)
    ===========================
    48500 iterations have been completed!
         --> Now loss =  tensor(0.0118, grad_fn=<NllLossBackward>)
    ===========================
    49000 iterations have been completed!
         --> Now loss =  tensor(0.0118, grad_fn=<NllLossBackward>)
    ===========================
    49500 iterations have been completed!
         --> Now loss =  tensor(0.0117, grad_fn=<NllLossBackward>)
    ===========================
    50000 iterations have been completed!
         --> Now loss =  tensor(0.0117, grad_fn=<NllLossBackward>)
    ===========================
    50500 iterations have been completed!
         --> Now loss =  tensor(0.0116, grad_fn=<NllLossBackward>)
    ===========================
    51000 iterations have been completed!
         --> Now loss =  tensor(0.0116, grad_fn=<NllLossBackward>)
    ===========================
    51500 iterations have been completed!
         --> Now loss =  tensor(0.0115, grad_fn=<NllLossBackward>)
    ===========================
    52000 iterations have been completed!
         --> Now loss =  tensor(0.0115, grad_fn=<NllLossBackward>)
    ===========================
    52500 iterations have been completed!
         --> Now loss =  tensor(0.0115, grad_fn=<NllLossBackward>)
    ===========================
    53000 iterations have been completed!
         --> Now loss =  tensor(0.0114, grad_fn=<NllLossBackward>)
    ===========================
    53500 iterations have been completed!
         --> Now loss =  tensor(0.0114, grad_fn=<NllLossBackward>)
    ===========================
    54000 iterations have been completed!
         --> Now loss =  tensor(0.0113, grad_fn=<NllLossBackward>)
    ===========================
    54500 iterations have been completed!
         --> Now loss =  tensor(0.0113, grad_fn=<NllLossBackward>)
    ===========================
    55000 iterations have been completed!
         --> Now loss =  tensor(0.0113, grad_fn=<NllLossBackward>)
    ===========================
    55500 iterations have been completed!
         --> Now loss =  tensor(0.0112, grad_fn=<NllLossBackward>)
    ===========================
    56000 iterations have been completed!
         --> Now loss =  tensor(0.0112, grad_fn=<NllLossBackward>)
    ===========================
    56500 iterations have been completed!
         --> Now loss =  tensor(0.0111, grad_fn=<NllLossBackward>)
    ===========================
    57000 iterations have been completed!
         --> Now loss =  tensor(0.0111, grad_fn=<NllLossBackward>)
    ===========================
    57500 iterations have been completed!
         --> Now loss =  tensor(0.0111, grad_fn=<NllLossBackward>)
    ===========================
    58000 iterations have been completed!
         --> Now loss =  tensor(0.0110, grad_fn=<NllLossBackward>)
    ===========================
    58500 iterations have been completed!
         --> Now loss =  tensor(0.0110, grad_fn=<NllLossBackward>)
    ===========================
    59000 iterations have been completed!
         --> Now loss =  tensor(0.0109, grad_fn=<NllLossBackward>)
    ===========================
    59500 iterations have been completed!
         --> Now loss =  tensor(0.0109, grad_fn=<NllLossBackward>)
    ===========================
    60000 iterations have been completed!
         --> Now loss =  tensor(0.0108, grad_fn=<NllLossBackward>)
    ===========================
    60500 iterations have been completed!
         --> Now loss =  tensor(0.0108, grad_fn=<NllLossBackward>)
    ===========================
    61000 iterations have been completed!
         --> Now loss =  tensor(0.0107, grad_fn=<NllLossBackward>)
    ===========================
    61500 iterations have been completed!
         --> Now loss =  tensor(0.0107, grad_fn=<NllLossBackward>)
    ===========================
    62000 iterations have been completed!
         --> Now loss =  tensor(0.0106, grad_fn=<NllLossBackward>)
    ===========================
    62500 iterations have been completed!
         --> Now loss =  tensor(0.0106, grad_fn=<NllLossBackward>)
    ===========================
    63000 iterations have been completed!
         --> Now loss =  tensor(0.0105, grad_fn=<NllLossBackward>)
    ===========================
    63500 iterations have been completed!
         --> Now loss =  tensor(0.0104, grad_fn=<NllLossBackward>)
    ===========================
    64000 iterations have been completed!
         --> Now loss =  tensor(0.0103, grad_fn=<NllLossBackward>)
    ===========================
    64500 iterations have been completed!
         --> Now loss =  tensor(0.0101, grad_fn=<NllLossBackward>)
    ===========================

```python
import matplotlib.pyplot as plt
plt.figure(figsize=(16,8))
length = loss_list.__len__()
print('The length of loss_list is:',length)
plt.plot(np.arange(0,4000,1),loss_list[:4000],'black')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
```

Softmax是一个产生激活作用的概率分布密度函数
为什么dim需要等于1？
首先dim=1意思是使得在softmax操作之后在dim这个维度相加等于1

```python
Probability_Calculator = nn.Softmax(dim=1)
pred = []
# x.detach()都是从原有计算中分离出来的一个tensor变量,但是没有更改这个tensor时，并不会影响backward()
# numpy()将tensor转变为array数组
prob = Probability_Calculator(DNN_Model.forward(x)).detach().numpy()
prob
```

    array([[9.9999940e-01, 5.8749430e-07],
           [9.9939322e-01, 6.0680578e-04],
           [2.8025184e-04, 9.9971968e-01],
           ...,
           [9.9690032e-01, 3.0997563e-03],
           [5.8808475e-04, 9.9941194e-01],
           [9.8336065e-01, 1.6639415e-02]], dtype=float32)

```python
Probability_Calculator = nn.Softmax(dim=1)   # softmax的dim这个参数 意思就是使得在softmax操作之后在dim这个维度相加等于1
pred = []
# x.detach()都是从原有计算中分离出来的一个tensor变量,但是没有更改这个tensor时，并不会影响backward()
# numpy()将tensor转变为array数组
prob = Probability_Calculator(DNN_Model.forward(x)).detach().numpy()
for i in range(prob.shape[0]):
    pred.append(np.argmax(prob[i,:]))

pred = np.array(pred)
pred.shape
accuracy_score(pred,y)
```

    0.997997997997998

## **Prediction，Backtesting，Computing Gain/loss**

```python
x = test_set.iloc[:,0:24]
y = test_set.iloc[:,24]
x = np.array(x)
y = np.array(y)
x = Variable(torch.FloatTensor(x))
y = Variable(torch.LongTensor(y))
```

```python
Probability_Calculator = nn.Softmax(dim=1)
pred = []
prob = Probability_Calculator(DNN_Model.forward(x)).detach().numpy()
for i in range(prob.shape[0]):
    if np.max(prob[i,:]) >= 0.95:
        pred.append(np.argmax(prob[i,:]))
    else:
        pred.append(np.nan)
pred = np.array(pred)
pred
```

    array([ 1.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,  0.,  1.,  1.,  1.,
            0.,  1.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,
            0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,
            0.,  1.,  0.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  0., nan,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,
            1.,  1.,  1.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  1.,  1.,
            1.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            1.,  1.,  0.,  0.,  0.,  1.,  0.,  1., nan, nan,  1.,  1.,  0.,
            0.,  0.,  0., nan,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  1.,  0.,
            1.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,
            0.,  0.,  0.,  1.,  1., nan,  1.,  0.,  1., nan,  1.,  0.,  0.,
           nan,  0.,  1.,  0.,  0.,  1.,  0.,  1.,  0.,  1.,  0., nan,  1.,
            1.,  1.,  1.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  1.,  1.,  1.,
            1.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0., nan,
            0.,  0.,  0.,  1.,  1.,  1., nan,  0.,  0.,  0.,  1.,  0.,  1.,
            1.,  0.,  1.,  0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
            0.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,
            0.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  1.,  1., nan,  0.,  0.,
            1.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  1.,  1.,  0.,  1.,  1.,
            1.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  0.,  1.,  0.,  1.,
            0.,  0.,  1.,  1.,  0.,  0.,  0., nan,  1.,  0.,  1.,  1.,  1.,
            1.,  0.,  1.,  1.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  1.,  0.,
            0.,  0.,  0.,  1., nan,  1.,  0., nan,  0.,  1.,  1.,  0.,  1.,
            1.,  0.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,
            1.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,
            1.,  1.,  0.,  0.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  0.,  1.,
            0.,  1.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,
            1.,  0.,  1.,  0.,  0.,  1.,  1.,  1.,  0.,  1.,  0.,  0.,  0.,
            1.,  1.,  0.,  0.,  1.,  1.,  1.,  1., nan,  1.,  1., nan,  0.,
            1.,  1.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,
           nan,  1.,  0.,  1.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,  0.,
            0.,  0.,  1.,  1.,  1.,  0.,  1.,  0.,  1.,  0.,  1.,  1., nan,
            1.,  0.,  1.,  0.,  1.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  1.,
           nan,  0.,  0.,  1.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  1.,  1.,
            0.,  1.,  1.,  0.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  1.,  1.,
            1.,  0.,  0.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  1., nan,  0.,
            0.,  0., nan,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  1.,  1.,
            0.,  1.,  0.,  0., nan,  1.,  0.,  1., nan,  0.,  0., nan,  1.,
            0.,  0.,  0., nan,  0.,  1.,  0., nan,  1.,  0.,  0.,  1.,  1.,
            0.,  0.,  1.,  1.,  0.,  1.,  0., nan, nan,  0.,  0.,  1.,  0.,
            0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,
            1.,  0.,  1.,  1.,  1.,  1.,  1.,  1., nan,  1.,  1.,  1.,  0.,
            0.,  1.,  1.,  1.,  1., nan,  1.,  0.,  0.,  1.,  0.,  0.,  1.,
            1., nan,  0.,  1., nan,  0.,  1.,  1.,  0.,  1.,  0., nan, nan,
           nan,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  1., nan,  0.,  1.,  1.,
            1.,  1.,  0.,  1.,  0., nan,  0.,  0.,  1.,  0.,  1.])

```python
pred.shape
```

    (596,)

```python
hs300 = np.exp(test_set['Close0'])
pred = 2 * pred-1
rtn_temp = 1.0
cum_rtn = []
cum_rtn.append(rtn_temp)
for i in range(pred.shape[0]-1):
    if pred[i] == 1: #  or pred[i] == -1
        rtn_temp = rtn_temp * (1.0 + ((hs300[i+1]-hs300[i]) / hs300[i]) * pred[i])
    else:
        rtn_temp = rtn_temp
    cum_rtn.append(rtn_temp)
```

```python
cum_rtn = np.array(cum_rtn)
cum_rtn
```

    array([1.        , 1.02504744, 1.0273072 , 1.0273072 , 1.02202574,
           0.99750313, 0.99750313, 0.98316189, 0.98316189, 0.98316189,
           0.98316189, 0.98675221, 0.98832743, 1.01247292, 1.01247292,
           1.00842323, 0.99785831, 0.99291828, 0.99291828, 0.99291828,
           0.97350798, 0.96310033, 0.96745702, 0.95341585, 0.95341585,
           0.94685415, 0.95707076, 0.95707076, 0.95707076, 0.95707076,
           0.96967293, 0.968985  , 0.99836371, 0.98937973, 1.00033966,
           1.00033966, 1.01070577, 0.96721925, 0.96656972, 0.96448592,
           0.96448592, 0.9788786 , 0.9788786 , 0.97090092, 0.97631122,
           0.97631122, 0.97631122, 1.01845365, 0.99140925, 0.99289476,
           0.99479758, 0.98836037, 0.95822971, 0.96862206, 0.98218786,
           0.98218786, 0.98218786, 0.97400558, 0.96812782, 0.96812782,
           0.96812782, 0.96812782, 0.96812782, 0.96812782, 0.96812782,
           0.97943444, 0.98405617, 0.99521174, 0.97219675, 0.97219675,
           0.97219675, 0.9507381 , 0.95006061, 0.95006061, 0.95006061,
           0.95006061, 0.9606705 , 0.98739507, 0.98944196, 0.98468507,
           0.96338959, 0.96338959, 0.9522464 , 0.9522464 , 0.95549808,
           0.97028888, 0.95408169, 0.95266228, 0.94278668, 0.93154591,
           0.92440064, 0.9129429 , 0.91559488, 0.90929041, 0.90929041,
           0.90929041, 0.90929041, 0.89687417, 0.89687417, 0.91836099,
           0.91836099, 0.91836099, 0.9276359 , 0.92589121, 0.92589121,
           0.92589121, 0.92589121, 0.92589121, 0.92589121, 0.92589121,
           0.92589121, 0.9135911 , 0.91293133, 0.91808447, 0.91808447,
           0.91808447, 0.92102289, 0.92102289, 0.93065903, 0.93065903,
           0.94758766, 0.95442847, 0.97354088, 0.97500533, 0.95682432,
           0.98750048, 0.98750048, 0.99103751, 0.99103751, 1.01332118,
           1.0735887 , 1.0735887 , 1.0735887 , 1.0735887 , 1.09709468,
           1.11008236, 1.11008236, 1.11941447, 1.11941447, 1.07495648,
           1.07495648, 1.08227666, 1.08227666, 1.08227666, 1.08227666,
           1.08227666, 1.07727797, 1.07727797, 1.07727797, 1.07641039,
           1.07641039, 1.06421934, 1.06421934, 1.05995779, 1.05995779,
           1.05995779, 1.05925363, 1.07282207, 1.08350986, 1.08217622,
           1.08217622, 1.08494311, 1.06150419, 1.05912498, 1.05912498,
           1.05912498, 1.05912498, 1.05519657, 1.06777213, 1.04315482,
           1.04144456, 1.04431572, 1.04431572, 1.04431572, 1.04431572,
           1.04431572, 0.98331206, 0.98331206, 0.98331206, 0.98331206,
           1.01902838, 1.01902838, 1.01902838, 1.01902838, 1.01902838,
           1.01902838, 1.01037906, 1.02406102, 1.01920154, 1.01920154,
           1.01920154, 1.01920154, 1.01920154, 1.01688407, 1.01688407,
           1.01370315, 1.01432314, 1.01432314, 1.01393697, 1.01393697,
           1.01393697, 1.01393697, 1.00625464, 1.004698  , 0.99638047,
           0.99636411, 0.9998536 , 1.01302643, 1.04371759, 1.04371759,
           1.04571304, 1.03483469, 1.03293452, 1.03293452, 1.03044836,
           1.06013685, 1.06050318, 1.06050318, 1.06050318, 1.06600681,
           1.04125142, 1.03860639, 1.03860639, 1.03860639, 1.03860639,
           1.03860639, 1.03389433, 1.03329684, 1.03329684, 1.03329684,
           1.02616571, 1.02839893, 1.02839893, 1.02839893, 1.02839893,
           1.02725288, 1.02725288, 1.02725288, 1.01871155, 1.01871155,
           0.99920689, 0.99920689, 0.99920689, 1.01241218, 1.00254546,
           1.00254546, 0.9935068 , 0.99801935, 1.00116324, 1.00116324,
           1.02289692, 1.02199034, 1.02037953, 1.02354987, 1.03092934,
           1.03092934, 1.0449017 , 1.0449017 , 1.04149708, 1.04149708,
           1.05485435, 1.05485435, 1.05485435, 1.06552776, 1.07182268,
           1.07182268, 1.07182268, 1.07182268, 1.07182268, 1.06786714,
           1.06786714, 1.07304289, 1.07696724, 1.08006006, 1.06771346,
           1.06771346, 1.05947518, 1.05130807, 1.05445832, 1.05445832,
           1.05445832, 1.05598601, 1.05598601, 1.06609444, 1.07740748,
           1.07277707, 1.07277707, 1.07277707, 1.07277707, 1.07277707,
           1.07693456, 1.07693456, 1.0768205 , 1.0768205 , 1.0768205 ,
           1.0768205 , 1.07158817, 1.07035445, 1.07035445, 1.07732244,
           1.08400879, 1.08400879, 1.085913  , 1.08077978, 1.08077978,
           1.08097639, 1.07994905, 1.08157729, 1.07361055, 1.07361055,
           1.07361055, 1.07361055, 1.07361055, 1.06267736, 1.06267736,
           1.06636009, 1.06636009, 1.06269515, 1.06269515, 1.06269515,
           1.06269515, 1.06269515, 1.0708493 , 1.07720645, 1.07529074,
           1.07529074, 1.07594413, 1.0727103 , 1.0727103 , 1.0727103 ,
           1.08730436, 1.08487784, 1.08336329, 1.08336329, 1.06983894,
           1.07681281, 1.07624919, 1.07624919, 1.07519058, 1.07519058,
           1.07912873, 1.0937908 , 1.09187308, 1.0877479 , 1.09589554,
           1.09589554, 1.10976638, 1.10945192, 1.12032475, 1.11656723,
           1.1103953 , 1.1103953 , 1.11195022, 1.11195022, 1.09295119,
           1.09295119, 1.09295119, 1.00681772, 1.0333724 , 1.04508015,
           1.04508015, 1.04510427, 1.04510427, 1.04510427, 1.04510427,
           1.03867537, 1.04596985, 1.04596985, 1.04596985, 1.04437158,
           1.06843592, 1.06968102, 1.06538888, 1.06538888, 1.05225705,
           1.05532106, 1.05532106, 1.05532106, 1.06094557, 1.06708871,
           1.06708871, 1.06708871, 1.06708871, 1.08994081, 1.07544468,
           1.07544468, 1.07544468, 1.02920838, 1.02419464, 1.00392432,
           0.9909013 , 0.9909013 , 0.95756312, 0.95756312, 0.98329372,
           0.97683003, 0.97683003, 0.97683003, 0.98003375, 0.98003375,
           0.99588728, 0.99020454, 0.99020454, 0.99020454, 0.99020454,
           0.99020454, 0.98602218, 1.00505296, 0.99760773, 0.99760773,
           1.00734406, 1.00734406, 0.9954733 , 0.9954733 , 0.99297644,
           0.98447725, 0.98447725, 0.99127089, 0.99127089, 1.00294713,
           1.00294713, 1.00005263, 1.00005263, 0.99918469, 0.99919983,
           0.99919983, 0.99919983, 0.99919983, 1.00177647, 1.01029036,
           1.01029036, 1.01029036, 1.01029036, 1.01168002, 1.02315921,
           1.01598372, 1.01889513, 1.01889513, 1.04638171, 1.04960411,
           1.04960411, 1.04921943, 1.05424132, 1.05424132, 1.06081534,
           1.05889661, 1.05889661, 1.06080459, 1.04806088, 1.04806088,
           1.04884929, 1.0558432 , 1.0558432 , 1.0558432 , 1.06092416,
           1.06535134, 1.05781741, 1.05781741, 1.05781741, 1.07974685,
           1.07974685, 1.14094374, 1.14094374, 1.15936881, 1.17558395,
           1.15430052, 1.1785443 , 1.1785443 , 1.1785443 , 1.1785443 ,
           1.1785443 , 1.1785443 , 1.1785443 , 1.18442566, 1.18392069,
           1.13195313, 1.13769632, 1.1476979 , 1.1476979 , 1.1476979 ,
           1.1572864 , 1.17608378, 1.17608378, 1.17640638, 1.17640638,
           1.17640638, 1.17640638, 1.16567776, 1.16567776, 1.16268558,
           1.16268558, 1.16268558, 1.16268558, 1.16268558, 1.14759491,
           1.14759491, 1.14759491, 1.14759491, 1.14759491, 1.14759491,
           1.1749838 , 1.1749838 , 1.1749838 , 1.17541331, 1.17541331,
           1.17541331, 1.15056321, 1.1567283 , 1.1567283 , 1.1567283 ,
           1.16816436, 1.17416514, 1.17416514, 1.16637156, 1.16637156,
           1.16637156, 1.16637156, 1.16637156, 1.16637156, 1.14399346,
           1.14399346, 1.14399346, 1.14646275, 1.14646275, 1.14646275,
           1.14646275, 1.15027545, 1.15027545, 1.15027545, 1.14858315,
           1.14858315, 1.15775637, 1.15760179, 1.15760179, 1.14318865,
           1.14318865, 1.14514788, 1.1544055 , 1.1630929 , 1.14418532,
           1.15039931, 1.16422361, 1.16422361, 1.18150438, 1.18165192,
           1.20478082, 1.20478082, 1.20478082, 1.20565526, 1.1929784 ,
           1.20460152, 1.20229752, 1.20229752, 1.21122442, 1.21122442,
           1.21122442, 1.2037853 , 1.2037853 , 1.2037853 , 1.21875557,
           1.21373448, 1.21373448, 1.21373448, 1.21132002, 1.21132002,
           1.21132002, 1.20833889, 1.19213567, 1.19213567, 1.17985604,
           1.17985604, 1.17985604, 1.17985604, 1.17985604, 1.17573857,
           1.18676001, 1.16746133, 1.17741989, 1.17741989, 1.17741989,
           1.17741989, 1.17242835, 1.17242835, 1.17242835, 1.18512389,
           1.20779722, 1.21885942, 1.24045511, 1.24045511, 1.22820502,
           1.22820502, 1.22820502, 1.22820502, 1.22820502, 1.24180554,
           1.24180554])

```python
time = test_set.index
time
```

    Index(['2018-08-08', '2018-08-09', '2018-08-10', '2018-08-13', '2018-08-14',
           '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-20', '2018-08-21',
           ...
           '2021-01-06', '2021-01-07', '2021-01-08', '2021-01-11', '2021-01-12',
           '2021-01-13', '2021-01-14', '2021-01-15', '2021-01-18', '2021-01-19'],
          dtype='object', name='date', length=596)

```python
fontTNR = 'Times New Roman'
plt.figure(figsize=(20,8))
plt.plot(time,cum_rtn,color='black')
plt.xticks(test_set.index[::30])
plt.grid()

plt.title('Backtesting Curve',fontsize = 15 , fontproperties = fontTNR)
plt.xlabel('Time',fontsize = 20 ,fontproperties = fontTNR)
plt.ylabel('Cumulative Return',fontsize = 20 ,fontproperties = fontTNR)

plt.xticks(fontsize = 15 , fontproperties = fontTNR,rotation=25)
plt.yticks(fontsize = 15 , fontproperties = fontTNR)
plt.show()
```

```python
DNN_Model.state_dict()
```

    OrderedDict([('FC_layer1.weight',
                  tensor([[-0.0861, -0.5635, -0.5108,  ...,  0.2102, -0.1364, -0.0351],
                          [ 0.3539,  0.0451, -0.6862,  ...,  0.1420,  0.3328,  0.3169],
                          [-0.0257, -0.1801,  0.1551,  ...,  0.1295, -0.0307,  0.3293],
                          ...,
                          [-0.6684, -0.7623, -0.8492,  ..., -0.4728, -0.2793, -0.3192],
                          [-0.0980,  0.4759,  0.2353,  ..., -0.3693, -0.1182, -0.0647],
                          [-0.3859,  0.1895, -0.0720,  ..., -0.2732, -0.2560, -0.2556]])),
                 ('FC_layer1.bias',
                  tensor([ 0.1029, -0.0978,  0.0527,  0.1733, -0.1503, -0.2079,  0.0588, -0.1384,
                           0.0311, -0.1918, -0.0311, -0.1319, -0.1119, -0.0931, -0.0207, -0.1940,
                           0.0717,  0.0722, -0.1249,  0.0102,  0.1022, -0.1759, -0.2364, -0.0517,
                          -0.0139,  0.0080,  0.0323,  0.1461,  0.0820, -0.1322,  0.1227, -0.0949,
                           0.0391,  0.1717, -0.2085, -0.1996,  0.2035, -0.0375, -0.1390,  0.0097,
                           0.0895,  0.1946, -0.1485,  0.1287,  0.0374,  0.0834, -0.0059, -0.0691])),
                 ('FC_layer2.weight',
                  tensor([[-0.2359,  0.4644, -0.4710,  ...,  0.1371,  0.0869,  0.1297],
                          [ 0.1311, -0.2098, -0.0183,  ..., -0.2080,  0.0376, -0.2414],
                          [-0.0874, -0.2564, -0.1380,  ...,  0.0240,  0.0213, -0.1071],
                          ...,
                          [-0.0534, -0.0920, -0.1771,  ..., -0.1108, -0.0409, -0.0428],
                          [-0.0476, -0.1462, -0.2311,  ..., -0.0941, -0.1929, -0.1422],
                          [ 0.0466,  0.0469, -0.1201,  ..., -0.1148,  0.0119,  0.0596]])),
                 ('FC_layer2.bias',
                  tensor([ 0.0140, -0.0252, -0.1434,  0.1166,  0.2894,  0.0356, -0.0140,  0.0937,
                          -0.0209,  0.0754, -0.1831, -0.0963, -0.1933, -0.0041, -0.0215,  0.0584,
                          -0.1314,  0.2693, -0.1370,  0.0254, -0.2048, -0.0788, -0.0190, -0.0352,
                           0.0127, -0.1445, -0.1950,  0.1544, -0.0668,  0.0474, -0.1389, -0.3142,
                           0.1560, -0.0246, -0.1696, -0.0040])),
                 ('FC_layer3.weight',
                  tensor([[-1.5763e-01,  1.8868e-02, -1.5444e-01, -1.6050e-02, -8.5780e-02,
                            4.8827e-02,  1.7089e-01, -5.0028e-02, -1.8344e-01, -1.2989e-01,
                           -1.5731e-02,  1.3388e-01, -1.9808e-02,  1.7498e-01, -1.9790e-01,
                           -9.7508e-02, -1.9234e-01,  2.0517e-01, -1.9292e-01, -8.1862e-02,
                           -1.9776e-01, -8.2192e-02, -5.8181e-02,  1.3059e-01,  6.8316e-03,
                           -3.0491e-02, -1.7550e-01, -7.1686e-02,  4.6621e-02, -1.6485e-01,
                           -9.9171e-02, -2.2526e-01, -5.9426e-02, -1.3636e-01, -1.3918e-01,
                            7.2536e-02],
                          [-4.0250e-01,  1.6652e-01,  1.2620e-01,  3.1609e-01,  3.2940e-01,
                            1.6882e-01,  1.7309e-01,  8.1675e-02,  1.5435e-02,  3.9453e-02,
                           -3.2551e-01,  1.4842e-01,  8.7654e-02,  2.6551e-01, -3.4158e-01,
                           -2.0640e-01, -3.4906e-01,  4.2143e-01, -5.6415e-02, -1.1820e-01,
                           -3.0409e-01, -4.5581e-02, -7.7152e-02,  1.7091e-01, -1.6446e-02,
                            2.6684e-02, -3.6083e-01,  3.0810e-01, -3.5481e-01, -3.6783e-02,
                           -1.4129e-02, -5.4736e-01,  3.6685e-01,  4.5680e-02, -9.9722e-02,
                           -4.4551e-02],
                          [-4.0733e-02,  6.4404e-02,  2.2875e-02,  3.3654e-01,  3.8175e-02,
                           -1.6680e-02,  3.3439e-01, -3.8119e-03, -1.1310e-01, -4.0748e-02,
                           -2.8271e-01,  2.2232e-04,  6.1828e-02,  2.2192e-01, -2.7651e-01,
                           -1.3772e-01, -2.7409e-01,  1.7964e-01, -2.5535e-01, -1.0441e-01,
                           -1.1541e-01,  2.5392e-02,  8.5710e-02, -5.9037e-02, -3.1696e-02,
                           -1.6340e-01, -2.5830e-01,  9.4642e-02, -3.2512e-01, -1.9128e-02,
                            7.2878e-02, -3.8466e-01,  4.2642e-01,  1.4116e-02, -6.4068e-02,
                            1.0759e-01],
                          [-1.0792e-01, -6.4718e-02,  2.9305e-02,  4.5725e-02, -8.7402e-02,
                            1.7677e-01, -1.1388e-01, -1.5855e-01, -5.2758e-03, -1.0293e-02,
                           -1.5989e-01, -4.7772e-02, -3.6837e-03,  1.3837e-01, -1.3800e-01,
                            9.1490e-02, -1.4130e-01,  1.1627e-01, -1.7613e-01, -1.4890e-01,
                           -1.1985e-01, -6.1589e-02, -1.5684e-01,  7.8940e-02, -1.9377e-01,
                            6.4707e-02, -6.6567e-02,  1.6677e-01, -1.7708e-01, -1.3520e-01,
                           -9.7980e-02, -2.4255e-01,  2.4082e-01, -6.5259e-02, -4.9521e-02,
                           -1.8615e-02],
                          [ 2.5957e-01, -7.5980e-02,  9.6908e-03, -2.4372e-01, -4.8096e-01,
                           -2.4781e-01, -2.2816e-01, -1.3493e-02, -9.3710e-02, -1.4484e-01,
                            2.4468e-01,  6.8252e-02,  3.5550e-02, -2.3865e-01,  3.4078e-01,
                            5.4542e-02,  2.6003e-01, -4.2743e-01,  1.9571e-01, -2.1898e-01,
                            3.6585e-01, -1.3748e-01, -2.3727e-01, -1.4570e-01, -3.0610e-02,
                            1.5240e-02,  3.6015e-01, -8.9189e-02,  1.4875e-01,  3.0533e-02,
                            3.0456e-03,  4.5884e-01, -6.1494e-01,  2.7697e-02,  2.0080e-02,
                            1.6710e-03],
                          [-2.4429e-01,  1.2640e-01,  1.3220e-01,  2.2486e-01,  1.5426e-01,
                            1.5394e-01,  2.7562e-01,  1.4497e-02, -2.6055e-02,  1.2952e-03,
                           -3.0473e-01, -4.2380e-02, -4.5484e-03,  1.4820e-01, -2.7164e-01,
                            5.3840e-02, -1.9368e-01,  4.2370e-01, -5.8003e-02, -6.1301e-02,
                           -3.3806e-01,  6.2851e-02,  1.3354e-01,  1.7504e-01,  2.8834e-02,
                           -1.5985e-01, -3.7975e-01,  3.0278e-02, -3.6712e-01,  5.4724e-02,
                            3.7224e-02, -3.1202e-01,  4.9635e-01, -1.1616e-01,  7.0275e-02,
                            6.0680e-03],
                          [-2.4788e-01,  1.3572e-01,  1.3666e-01,  1.8277e-01,  1.7210e-01,
                            1.1230e-01,  4.3597e-01, -2.1358e-03, -6.0739e-02, -5.7898e-02,
                           -3.6411e-01, -5.8389e-03,  7.0215e-02,  2.1818e-01, -2.8247e-01,
                            1.0756e-01, -1.4074e-01,  5.8754e-01, -2.6964e-01,  2.0395e-01,
                           -3.2725e-01, -1.7093e-02,  6.9262e-02,  1.0324e-01, -6.2924e-02,
                           -9.9214e-02, -4.3801e-01,  2.3702e-01, -3.7086e-01,  5.7940e-02,
                           -1.2366e-01, -3.6793e-01,  6.0831e-01, -5.7057e-02, -1.0810e-01,
                           -2.0857e-01],
                          [-2.4936e-01,  1.1129e-01, -8.5846e-02,  1.1629e-01,  2.3204e-01,
                           -8.3728e-02, -5.0009e-03, -3.6876e-02, -5.6730e-02,  8.9288e-02,
                           -3.0675e-01, -2.0990e-02,  2.0179e-03,  1.3768e-01, -8.8847e-02,
                           -1.2821e-01, -2.4594e-01,  2.9177e-02,  1.1291e-02, -7.5023e-03,
                           -1.9662e-01, -1.1896e-01,  8.1231e-02,  9.6271e-02, -2.1776e-01,
                            4.3825e-02, -1.1745e-01,  1.5375e-01, -2.7449e-01,  8.7305e-02,
                           -6.9382e-02, -2.9008e-01,  3.5329e-02,  3.8964e-02,  1.5343e-01,
                            6.9381e-02],
                          [ 5.0705e-01,  7.5225e-02, -1.9947e-01, -7.2118e-01, -7.1222e-01,
                           -4.6810e-01, -6.6338e-01, -2.6533e-01,  9.3366e-03,  1.5560e-01,
                            6.0585e-01,  2.8834e-02,  8.7921e-02, -1.2617e-01,  3.7753e-01,
                            1.7388e-01,  3.9981e-01, -8.5104e-01,  2.9579e-01, -3.6954e-02,
                            3.4440e-01, -1.4266e-01, -2.7917e-01, -2.8903e-01,  2.2271e-01,
                            2.7432e-01,  7.6578e-01, -3.1120e-01,  5.8330e-01, -2.0584e-01,
                           -1.1826e-01,  8.7565e-01, -8.4367e-01, -1.4038e-01, -2.1993e-01,
                            1.6820e-01],
                          [ 6.1618e-01, -2.0181e-01, -1.1586e-01, -6.5962e-01, -7.5546e-01,
                           -2.4145e-01, -7.1842e-01, -3.0258e-01,  1.8481e-01, -3.1264e-02,
                            7.1100e-01, -2.0891e-01, -9.7056e-02, -3.6655e-01,  3.9227e-01,
                            1.1348e-01,  4.8274e-01, -1.0601e+00,  6.0539e-01, -2.4429e-01,
                            7.6612e-01, -1.0369e-02, -1.8387e-01, -3.5977e-01,  1.6472e-01,
                            2.1449e-01,  9.6197e-01, -2.1844e-01,  5.0461e-01, -1.2234e-01,
                            6.7183e-02,  9.8275e-01, -1.0813e+00,  6.9863e-02, -2.2164e-01,
                            9.3208e-02],
                          [-5.7226e-01,  1.8769e-01, -5.3923e-02,  7.7387e-01,  7.7540e-01,
                            3.4125e-01,  5.3061e-01,  2.2552e-01, -1.8166e-01,  5.1604e-02,
                           -6.1244e-01,  2.0704e-01,  5.5774e-02,  3.6524e-01, -2.9256e-01,
                           -1.9852e-01, -5.6431e-01,  9.7928e-01, -5.5009e-01,  2.2640e-01,
                           -4.6449e-01,  1.4530e-01,  1.9968e-01,  3.0306e-01, -2.6307e-01,
                           -1.7454e-01, -7.3779e-01,  3.9801e-01, -6.5023e-01,  4.2400e-02,
                            1.5837e-01, -9.8155e-01,  8.9757e-01,  3.9165e-02,  2.0116e-01,
                           -2.7800e-01],
                          [ 6.4124e-01, -2.9317e-01, -2.5212e-01, -1.0591e+00, -8.2375e-01,
                           -5.8117e-01, -9.2188e-01, -1.4222e-01,  4.4446e-02,  1.3393e-01,
                            1.0480e+00, -2.2451e-01, -7.7949e-02, -5.0875e-01,  6.1654e-01,
                            1.5854e-01,  4.8515e-01, -1.3993e+00,  4.7638e-01, -2.7428e-01,
                            9.0608e-01, -1.0175e-01, -2.6520e-01, -2.0782e-01,  3.1496e-01,
                            1.1251e-01,  1.1635e+00, -3.7749e-01,  8.8009e-01, -1.9646e-01,
                            5.0011e-02,  1.3022e+00, -1.2342e+00, -9.8339e-02, -1.1810e-01,
                            2.1539e-01],
                          [ 7.1685e-03, -1.2686e-01, -1.1366e-01,  5.2048e-02,  8.9431e-02,
                            1.3545e-01, -3.1158e-02, -4.1021e-02, -5.7851e-02, -1.8050e-01,
                           -1.5466e-02, -1.6666e-01,  1.2606e-01, -7.7843e-02, -2.1250e-01,
                           -1.3819e-01, -8.1280e-02,  1.5420e-01, -2.6689e-01,  5.2791e-02,
                           -1.0808e-01, -1.3916e-01,  6.3449e-02, -5.7353e-03, -6.5060e-02,
                            1.8268e-02, -9.4458e-02,  3.9187e-02, -1.6682e-01,  3.6127e-02,
                           -3.4982e-03, -3.2648e-01,  2.2889e-01,  7.3718e-02,  9.1869e-02,
                           -2.3548e-02],
                          [-5.1995e-02,  9.5385e-02, -1.2733e-01,  3.0907e-01,  1.8886e-01,
                            1.3072e-01,  4.0072e-02, -4.8568e-02, -3.5790e-02, -9.3972e-02,
                           -1.7419e-01, -9.5049e-02, -8.9847e-02, -7.3447e-02, -3.1358e-01,
                           -1.7438e-01, -1.5253e-01,  3.3339e-01, -4.8277e-02, -8.9651e-02,
                           -3.3702e-01,  3.4778e-02,  1.0201e-02, -3.6210e-02, -8.2409e-02,
                           -7.3123e-02, -3.3187e-01,  4.4632e-02, -7.6358e-02,  4.2277e-02,
                            2.2176e-02, -2.3865e-01,  3.6393e-01,  8.5710e-03,  1.7082e-01,
                           -1.0595e-01],
                          [ 9.8232e-02,  5.8383e-02,  1.2162e-02, -1.7030e-01, -1.9584e-01,
                           -2.0903e-03, -3.4812e-02, -1.7809e-01, -2.7124e-02, -1.9780e-01,
                           -8.0016e-03,  1.9516e-02, -3.0012e-02,  8.5232e-02, -1.2433e-01,
                           -7.3589e-02,  1.0981e-01, -2.0920e-01, -7.4301e-02,  5.8491e-02,
                            1.2996e-01,  1.0762e-01, -5.5863e-02, -1.8387e-01, -1.9297e-01,
                            1.0802e-01, -2.3705e-02, -6.5813e-02,  2.9840e-02, -1.8421e-01,
                           -2.5832e-02,  1.0185e-01, -2.0740e-02,  1.0743e-01,  1.5129e-03,
                           -8.9038e-02],
                          [-5.0179e-01,  3.3886e-02, -7.9031e-02,  4.5581e-01,  4.0854e-01,
                            3.0781e-01,  5.2242e-01,  3.1661e-01, -5.0472e-02,  1.7067e-01,
                           -5.6787e-01, -8.0417e-02,  1.3836e-01,  1.4746e-01, -1.9306e-01,
                           -1.2671e-01, -2.6568e-01,  8.4973e-01, -3.4238e-01,  1.6674e-01,
                           -5.3509e-01,  8.3736e-02,  2.0266e-01,  1.8251e-01,  4.1420e-02,
                           -2.3186e-02, -6.4609e-01,  3.0461e-01, -5.0640e-01, -8.1449e-02,
                            1.4625e-02, -7.1546e-01,  8.5928e-01, -6.4718e-02,  2.4350e-01,
                            7.5128e-03],
                          [-2.2285e-01,  1.0857e-02, -1.8248e-01, -3.0123e-02,  1.0723e-01,
                           -1.3167e-01,  3.3189e-02, -8.3115e-02, -1.7852e-01,  7.7904e-02,
                           -1.9266e-01, -8.3915e-02,  3.2321e-02, -3.3958e-02, -1.8725e-01,
                           -1.1478e-01,  5.6716e-02, -6.9924e-02, -2.1040e-01,  1.0919e-01,
                           -2.2569e-01, -1.8240e-01, -1.5931e-02, -1.3928e-01, -7.4190e-02,
                            4.7199e-02, -3.2381e-02, -1.2039e-01, -2.4302e-01, -9.6816e-02,
                            3.3323e-02, -1.8928e-01,  1.9644e-01,  1.3776e-01, -5.1122e-02,
                            1.0238e-02],
                          [ 3.1542e-01, -1.0071e-01, -3.3809e-02, -3.1980e-01, -1.4546e-01,
                           -6.3505e-02, -2.0994e-01, -2.1517e-01, -6.4286e-02,  8.2350e-02,
                            2.7637e-01, -1.6297e-01, -7.3031e-03, -2.0101e-01,  6.0403e-02,
                           -7.4647e-02,  1.1780e-01, -4.6727e-01, -3.2734e-02, -2.2128e-01,
                            2.8653e-01, -1.6685e-01, -1.8050e-01, -2.5443e-01, -8.7459e-02,
                           -6.5968e-03,  4.1845e-01,  3.3995e-02,  2.4856e-01, -1.3881e-02,
                            1.4678e-01,  3.7109e-01, -3.6217e-01,  5.0945e-02, -1.7698e-01,
                           -4.1554e-02],
                          [-8.0044e-01,  6.0991e-02,  2.4030e-01,  1.0383e+00,  1.0808e+00,
                            6.2563e-01,  9.9345e-01,  4.0953e-01,  4.8197e-02, -9.8052e-02,
                           -1.0931e+00, -5.2888e-02,  1.0128e-01,  4.1904e-01, -7.3696e-01,
                           -6.1364e-02, -7.8063e-01,  1.4828e+00, -6.6958e-01,  1.6235e-01,
                           -7.8602e-01,  1.4799e-03,  1.6296e-01,  3.8643e-01, -1.3420e-01,
                           -1.1414e-01, -1.2031e+00,  5.6669e-01, -7.6980e-01,  1.6235e-01,
                            1.2398e-02, -1.4031e+00,  1.3588e+00,  1.0670e-01,  3.1032e-01,
                            1.8600e-02],
                          [-7.5362e-01,  5.5423e-02,  1.7893e-01,  7.6729e-01,  8.0874e-01,
                            5.3784e-01,  5.9990e-01,  2.1773e-01, -9.1679e-02,  1.9330e-02,
                           -8.4539e-01,  1.5457e-01,  1.3157e-01,  3.5714e-01, -4.8129e-01,
                           -1.0837e-01, -5.6057e-01,  1.0813e+00, -5.2564e-01,  1.5808e-01,
                           -7.7431e-01, -3.0707e-02,  8.7690e-02,  4.3069e-01, -3.4133e-02,
                           -1.1733e-01, -8.0619e-01,  4.1244e-01, -7.6783e-01,  2.2284e-01,
                            1.7605e-01, -1.1175e+00,  9.8434e-01,  1.1662e-01,  1.3052e-01,
                            1.7048e-02],
                          [ 5.6157e-01, -2.0774e-01, -2.7389e-01, -5.5774e-01, -8.0048e-01,
                           -4.6774e-01, -7.7437e-01, -1.5247e-01,  9.4347e-02, -1.0681e-01,
                            7.7070e-01, -2.4047e-05,  6.3275e-02, -1.6038e-01,  3.7863e-01,
                            1.7146e-01,  2.7598e-01, -1.0636e+00,  5.1521e-01, -2.3533e-01,
                            5.6459e-01,  1.2233e-01, -5.6879e-02, -2.3444e-01,  2.1186e-02,
                            1.6505e-01,  8.9362e-01, -4.9518e-01,  6.3610e-01, -1.9454e-01,
                           -1.0285e-01,  8.7712e-01, -1.0878e+00,  8.4451e-02, -7.3794e-03,
                            2.3018e-01],
                          [ 5.2103e-01,  9.1095e-02, -1.6315e-01, -5.3979e-01, -5.6779e-01,
                           -2.4636e-01, -6.2370e-01, -1.0021e-01,  7.9842e-02, -4.2564e-02,
                            5.2584e-01,  7.6305e-02, -6.6741e-02, -3.5544e-01,  2.4199e-01,
                           -4.1998e-02,  3.6420e-01, -8.5261e-01,  4.7736e-01, -9.3127e-02,
                            3.7144e-01,  6.1179e-02, -2.0282e-01, -3.1959e-01, -3.3077e-02,
                            1.1341e-01,  7.1937e-01, -1.6487e-01,  5.2345e-01, -1.4580e-01,
                            1.1917e-01,  8.5992e-01, -7.1978e-01, -8.4026e-02, -7.5979e-02,
                            2.1359e-01],
                          [-4.5117e-01, -4.0728e-02,  1.6340e-02,  6.0485e-01,  5.7580e-01,
                            1.3570e-01,  5.0679e-01,  1.4862e-01, -1.0927e-01, -6.9882e-02,
                           -4.3197e-01,  9.0975e-04,  1.6692e-01,  8.4681e-02, -4.5209e-01,
                           -1.8310e-01, -3.6066e-01,  7.6379e-01, -2.7570e-01,  2.2056e-01,
                           -3.9911e-01,  1.4316e-01,  2.3895e-01,  2.4606e-01,  3.6183e-02,
                           -2.8173e-01, -5.7073e-01,  3.0715e-01, -4.4968e-01,  1.9184e-01,
                            8.4140e-02, -5.9109e-01,  4.7188e-01,  1.3540e-01,  3.5808e-02,
                           -5.8094e-02],
                          [ 2.5161e-02,  7.2590e-02, -1.4838e-01, -3.2675e-01, -3.1266e-01,
                           -2.1411e-01, -9.1318e-02, -1.1337e-01, -1.0898e-01, -1.4817e-01,
                            1.9330e-01,  1.0813e-01,  4.8441e-02, -1.0025e-01,  1.0866e-01,
                            5.5700e-02,  1.0385e-01, -3.6523e-01,  2.7763e-01, -2.0037e-01,
                            1.1823e-01,  1.5736e-01, -1.8448e-01,  5.2477e-02,  1.2036e-01,
                            1.3390e-01,  2.3679e-01, -1.6376e-01,  4.2499e-02, -4.8060e-02,
                           -1.2852e-02,  3.2739e-01, -3.4038e-01,  7.8442e-02, -3.8892e-02,
                           -8.5504e-02]])),
                 ('FC_layer3.bias',
                  tensor([-0.1451,  0.0079, -0.0115,  0.0242,  0.1208, -0.1654, -0.0135, -0.1698,
                           0.1641, -0.0451, -0.0242, -0.0379,  0.0152, -0.1718, -0.1689, -0.0997,
                           0.0055,  0.1207, -0.1417, -0.0443,  0.1627, -0.0373, -0.1651, -0.1239])),
                 ('FC_layer4.weight',
                  tensor([[-6.2032e-02, -1.4713e-01, -8.9856e-02,  7.9218e-02, -1.2283e-01,
                           -2.5157e-01, -1.2653e-01, -2.1432e-01, -1.3992e-01, -1.1738e-01,
                           -1.6877e-01, -4.8216e-02,  8.0311e-02,  1.0261e-01, -5.9118e-02,
                            7.0779e-02, -1.3477e-02, -2.6327e-01, -1.7546e-01,  6.8196e-03,
                           -2.3325e-01, -7.9117e-02, -1.0051e-01, -1.0722e-01],
                          [ 2.1639e-01,  3.2000e-01,  5.0037e-01,  3.1543e-01, -5.6504e-01,
                            3.7775e-01,  4.9284e-01,  2.9385e-01, -1.0408e+00, -1.3783e+00,
                            1.1402e+00, -1.6549e+00,  3.1825e-01,  1.7420e-01, -8.8789e-02,
                            9.6651e-01,  2.5790e-01, -2.7491e-01,  1.5444e+00,  1.2605e+00,
                           -1.1991e+00, -9.5089e-01,  7.8119e-01, -5.0550e-01],
                          [ 6.0516e-02,  7.2191e-02,  1.5802e-01,  9.2519e-02, -3.3708e-02,
                            1.2478e-01, -1.6773e-01, -1.9157e-02, -2.4154e-01, -3.6417e-01,
                            1.8787e-01, -1.9966e-01, -7.0878e-02, -2.1687e-01,  3.0925e-02,
                           -3.3352e-02,  2.8121e-02, -3.6361e-01,  4.7108e-02,  2.5743e-01,
                           -3.8639e-01, -4.9703e-02,  2.0170e-01, -4.7837e-02],
                          [ 9.3415e-02,  3.9038e-01,  4.5644e-01,  1.5741e-02, -4.7028e-01,
                            4.5103e-01,  5.4163e-01,  1.3746e-01, -6.6838e-01, -9.9655e-01,
                            5.8240e-01, -1.1682e+00, -4.7298e-02,  2.9966e-01, -2.1743e-01,
                            5.7887e-01,  3.6047e-02, -2.6815e-01,  1.2470e+00,  9.5769e-01,
                           -8.3629e-01, -7.4676e-01,  3.3960e-01, -2.7330e-01],
                          [ 3.0543e-02, -4.8916e-01, -2.0580e-01, -2.7168e-01,  5.3850e-01,
                           -4.9789e-01, -6.1361e-01, -1.6785e-01,  9.1953e-01,  1.1141e+00,
                           -9.3372e-01,  1.4649e+00, -1.7113e-01, -4.0015e-01, -5.8513e-02,
                           -6.3465e-01, -1.6501e-01,  6.0229e-01, -1.6570e+00, -1.2590e+00,
                            1.1666e+00,  9.2976e-01, -7.0111e-01,  3.5051e-01],
                          [ 1.0633e-01, -2.8454e-01, -2.8118e-01, -2.0132e-01, -5.5819e-02,
                           -7.8870e-02,  1.9714e-02, -4.2597e-02,  1.4777e-01,  3.7150e-01,
                           -4.2315e-01,  4.3685e-01,  6.3831e-02,  6.1570e-02, -1.6156e-01,
                           -3.5434e-01, -2.4723e-01,  6.2488e-02, -4.9554e-01, -2.4095e-01,
                            3.3478e-01,  2.1917e-02, -2.4287e-01,  2.9870e-02],
                          [ 5.9556e-02, -1.3503e-01, -2.0922e-02, -8.6814e-02,  4.0498e-02,
                           -9.0152e-02, -2.5226e-01, -2.2193e-01, -2.0231e-01,  2.8868e-02,
                            1.1160e-01, -1.0689e-01, -2.7715e-01,  1.1711e-01, -1.5687e-01,
                           -4.7492e-02, -2.4792e-01, -7.1277e-02,  2.0550e-02,  1.3740e-02,
                           -1.7675e-01, -6.1213e-03,  8.1961e-02, -2.8159e-01],
                          [-2.3162e-01, -1.4717e-01,  5.5652e-02, -1.9490e-01,  8.4206e-03,
                           -2.9278e-01, -5.4586e-02,  9.9816e-03,  2.1621e-01,  5.3825e-01,
                           -2.9789e-01,  7.0983e-01, -2.4044e-02, -5.8823e-02,  4.8796e-02,
                           -2.0613e-01, -9.5606e-03,  7.5029e-02, -7.2448e-01, -5.2746e-01,
                            3.4836e-01,  2.1013e-01, -1.9923e-01, -1.2923e-02],
                          [-2.0249e-02,  3.0672e-01,  2.1209e-01,  8.0248e-02, -4.5953e-01,
                            1.8632e-01,  4.3298e-01,  2.1034e-01, -1.0567e+00, -8.2499e-01,
                            9.4889e-01, -1.0286e+00, -3.8023e-03,  4.1586e-01, -1.8947e-01,
                            7.6165e-01,  3.6232e-02, -3.0432e-01,  1.2662e+00,  9.8726e-01,
                           -9.8945e-01, -9.2847e-01,  8.2791e-01, -3.2166e-01],
                          [-1.6355e-01, -7.1227e-01, -2.1366e-01,  9.5916e-04,  6.9108e-01,
                           -5.4642e-01, -5.5108e-01, -2.3929e-01,  1.2650e+00,  1.2779e+00,
                           -1.2826e+00,  1.6277e+00, -3.4892e-01, -2.9999e-01,  1.8464e-01,
                           -1.0074e+00,  9.4502e-02,  6.4880e-01, -1.7828e+00, -1.3357e+00,
                            1.3621e+00,  8.7730e-01, -8.5667e-01,  3.9720e-01],
                          [ 1.4073e-01,  3.3857e-01,  1.0371e-01,  3.2180e-02, -8.3751e-02,
                            1.6141e-01,  1.1849e-01, -1.2933e-01, -4.7865e-01, -6.3727e-01,
                            3.3267e-01, -7.3078e-01,  2.0280e-02,  2.0269e-01, -1.8950e-01,
                            4.2790e-01,  1.0871e-01, -2.2768e-01,  6.7758e-01,  3.3401e-01,
                           -2.6517e-01, -4.0787e-01,  2.2386e-01, -8.4474e-03],
                          [-1.4583e-01, -4.2622e-01, -4.2170e-01,  4.7281e-02,  5.0980e-01,
                           -2.7019e-01, -4.9610e-01, -2.4811e-01,  6.1358e-01,  7.2919e-01,
                           -8.7790e-01,  1.2400e+00, -8.4429e-02, -1.6614e-01,  1.3565e-01,
                           -4.7334e-01, -1.6465e-01,  2.9387e-01, -1.1166e+00, -8.3574e-01,
                            7.6809e-01,  7.9646e-01, -5.7243e-01,  3.0396e-01]])),
                 ('FC_layer4.bias',
                  tensor([-0.2170, -0.2406, -0.0231,  0.0457,  0.2083,  0.0397, -0.1073, -0.0637,
                          -0.0361,  0.1476,  0.0649,  0.1438])),
                 ('FC_layer5.weight',
                  tensor([[ 0.2527, -2.5937, -0.5486, -1.9344,  2.5169,  0.8707, -0.3714,  0.9929,
                           -2.0736,  2.8293, -1.1125,  1.9031],
                          [ 0.3218,  2.6480,  0.2650,  1.7932, -2.3031, -0.3996, -0.1756, -0.7388,
                            2.0334, -2.7776,  0.8806, -1.6891]])),
                 ('FC_layer5.bias', tensor([-0.0505, -0.0314]))])

```python
optimizer.state_dict()
```

    {'state': {0: {'momentum_buffer': None},
      1: {'momentum_buffer': None},
      2: {'momentum_buffer': None},
      3: {'momentum_buffer': None},
      4: {'momentum_buffer': None},
      5: {'momentum_buffer': None},
      6: {'momentum_buffer': None},
      7: {'momentum_buffer': None},
      8: {'momentum_buffer': None},
      9: {'momentum_buffer': None}},
     'param_groups': [{'lr': 0.04210455402856372,
       'momentum': 0,
       'dampening': 0,
       'weight_decay': 0,
       'nesterov': False,
       'params': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}]}

```python

```

```python

```