R,keras,TypeError:“ MatMul”操作的输入“ b”的类型为float32,与参数“ a”的类型int32不匹配

时间:2019-12-09 22:31:53

标签: r tensorflow keras

我对keras / tensorflow非常陌生,在使用训练有素的模型进行预测时遇到错误,我也不知道为什么。我的目的是预测产品的需求(请忽略我称之为weather_data的情况)。下面是我使用的代码,收到的错误消息以及最后的产品每周需求数据(以矢量形式,为期151周):

max_len <- 6 # the number of previous examples we'll look at
batch_size <- 32 # number of sequences to look at at one time during training
total_epochs <- 15 # how many times we'll look @ the whole dataset while training our model

# set a random seed for reproducability
set.seed(123)

# Cut the text in overlapping sample sequences of max_len characters
  ## get a list of start indexes for our (overlapping) chunks
  start_indexes <- seq(1, length(Order_volume_vector) - (max_len + 1), by = 3)
  ## create an empty matrix to store our data in
  weather_matrix <- matrix(nrow = length(start_indexes), ncol = max_len + 1)
  ## fill our matrix with the overlapping slices of our dataset
  for (i in 1:length(start_indexes)){
    weather_matrix[i,] <- Order_volume_vector[start_indexes[i]:(start_indexes[i] + max_len)]
  }

# split our data into the day we're predict (y), and the 
# sequence of days leading up to it (X)
X <- weather_matrix[,-ncol(weather_matrix)]
y <- weather_matrix[,ncol(weather_matrix)]

# create an index to split data into testing & training sets
training_index <- createDataPartition(y, p = .9, 
                                      list = FALSE, 
                                      times = 1)
# training data
X_train <- array(X[training_index,], dim = c(length(training_index), max_len, 1))
y_train <- y[training_index]

# testing data
X_test <- array(X[-training_index,], dim = c(length(y) - length(training_index), max_len, 1))
y_test <- y[-training_index]

# initialize model
model <- keras_model_sequential()
model %>%
  layer_dense(input_shape = dim(X_train)[2:3], units = max_len)
model %>% 
  layer_simple_rnn(units = 6, activation = 'linear')
model %>%
  layer_dense(units = 1) # output
summary(model)
model %>% compile(loss = 'mse', 
                  optimizer = 'adam', 
                  metrics = 'mae')

##Train model
trained_model <- model %>% fit(
  x = X_train, # sequence we're using for prediction 
  y = y_train, # sequence we're predicting
  batch_size = batch_size, # how many samples to pass to our model at a time
  epochs = total_epochs, # how many times we'll look @ the whole dataset
  validation_split = 0.1) #                   

当我现在尝试使用模型进行预测时,出现以下错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.

Detailed traceback: 
  File "C:\Users\JENNIF~1\AppData\Local\CONTIN~1\ANACON~1\envs\R-RETI~1\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 909, in predict
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\JENNIF~1\AppData\Local\CONTIN~1\ANACON~1\envs\R-RETI~1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 462, in predict
    steps=steps, callbacks=callbacks, **kwargs)
  File "C:\Users\JENNIF~1\AppData\Local\CONTIN~1\ANACON~1\envs\R-RETI~1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 444, in _model_iteration
    total_epochs=1)
  File "C:\Users\JENNIF~1\AppData\Local\CONTIN~1\ANACON~1\envs\R-RETI~1\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 123, in run_one_epoch
    batch_outs = execution_function(iterator)
  File "C:\Users\JENNIF~1\AppData\Local\

我尝试使用不同的tensorflow软件包等重新安装软件包,但似乎无济于事。在网上我只能找到python的解决方案,我不知道该如何在R中应用。这是原始数据:

1   1350
2   3700
3   3900
4   4500
5   3350
6   2900
7   3200
8   4550
9   4250
10  4050
11  4800
12  5600
13  4800
14  4450
15  3600
16  2450
17  4400
18  3550
19  4500
20  4600
21  3600
22  3550
23  4000
24  3000
25  4100
26  4050
27  4100
28  3700
29  4200
30  3700
31  4200
32  3050
33  2800
34  4400
35  4850
36  4700
37  3900
38  4550
39  7200
40  2700
41  8300
42  5900
43  5150
44  2600
45  2500
46  6750
47  7200
48  2600
49  5500
50  7700
51  8050
52  450
53  1950
54  3100
55  6250
56  3850
57  3250
58  7250
59  4100
60  3500
61  3700
62  4400
63  3400
64  3950
65  3300
66  3700
67  4600
68  5950
69  4550
70  2500
71  4400
72  6100
73  3950
74  2450
75  5750
76  4650
77  4750
78  4450
79  3800
80  7450
81  4150
82  3000
83  4600
84  2750
85  3650
86  7700
87  4450
88  5500
89  5150
90  4300
91  6350
92  4800
93  4200
94  5900
95  4600
96  4750
97  5200
98  6300
99  3500
100 10150
101 11150
102 12700
103 8000
104 250
105 900
106 2850
107 4200
108 6100
109 6400
110 5500
111 6250
112 8050
113 5200
114 7500
115 5850
116 7450
117 5250
118 5400
119 6000
120 4550
121 3350
122 3800
123 5200
124 9350
125 5500
126 3700
127 4500
128 3950
129 4400
130 4650
131 6500
132 7100
133 5300
134 4250
135 5150
136 5450
137 5800
138 6000
139 4850
140 5700
141 5450
142 5900
143 7400
144 6500
145 6200
146 8750
147 9450
148 5300
149 5600
150 6100
151 3750

0 个答案:

没有答案