LSTM模型可以训练怎样的数据集?

现有数据集Y={X1,X2,X3.....Xn},其中,X5与X4没有关系,但是X5与X2有关系,具有这种特点的数据集可以用LSTM模型进行训练吗?

1个回答

LSTM主要用来学习序列,并且序列的变化的影响因子和序列本身有关或者周期性波动。
比如说文本预测,每天每周每年的销量
别的也可以学,但是效果未必很好。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

tensorflow训练自己的数据集

tensorflow框架下进行VGG网络的训练,训练自己的数据集,生成模型,损失函数的计算一直保持0.69,这是为什么,应该怎样调整?

LSTM时间序列模型做销量预测多对多预测时的数据格式错误

最近对公司一款产品做销量预测,选用LSTM时间序列模型来探索,时间维度以月为单位,已经尝试了一对一,多对一的探索,效果不理想,预测未来三个月的销量,结果容易拟合成一条曲线,现在尝试用多对多的方式测试一下,思路是用前三个月的销量预测后三个月的销量,例如用1,2,3月份的数据预测4,5,6月销量,2,3,4对5,6,7,以此类推,但是在训练模型的时候遇到了数据格式的问题,现在有三年多的销售数据,数据已经处理成3个月一组的格式,格式如下: ``` X_train = X_train.reshape((X_train.shape[0],1,X_train.shape[1])) y_train = y_train.reshape((y_train.shape[0],1,y_train.shape[1])) -->X_train.shape (43, 1, 3) --> y_train.shape (43, 1, 3) -->X_train array([[[0. , 0.0040222 , 0.023007 ]], [[0.0040222 , 0.023007 , 0.03394739]], [[0.023007 , 0.03394739, 0.00530931]], [[0.03394739, 0.00530931, 0.04529 ]], [[0.00530931, 0.04529 , 0.0635508 ]], [[0.04529 , 0.0635508 , 0.05301263]]......] -->y_train array([[[0.03394739, 0.00530931, 0.04529 ]], [[0.00530931, 0.04529 , 0.0635508 ]], [[0.04529 , 0.0635508 , 0.05301263]], [[0.0635508 , 0.05301263, 0.10578393]], [[0.05301263, 0.10578393, 0.08824712]].....] ``` 模型信息如下: ``` from keras.models import Sequential from keras.layers import Dense import keras.backend as K from keras.callbacks import EarlyStopping from keras.layers import LSTM model_lstm = Sequential() model_lstm.add(LSTM(10,input_shape=(1, X_train.shape[2]),activation='sigmoid',kernel_initializer='lecun_uniform',return_sequences=True)) model_lstm.add(Dense(1)) model_lstm.compile(loss='mean_squared_error', optimizer='adam') early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1) history_model_lstm = model_lstm.fit(X_train, y_train, epochs=500, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop]) ``` 最后提示目标值的格式貌似有问题,不知道怎么搞,求大神指点一二。。 ``` ValueError Traceback (most recent call last) <ipython-input-40-c6184d374dfd> in <module> 10 model_lstm.compile(loss='mean_squared_error', optimizer='adam') 11 early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1) ---> 12 history_model_lstm = model_lstm.fit(X_train, y_train, epochs=500, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop]) c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 1152 sample_weight=sample_weight, 1153 class_weight=class_weight, -> 1154 batch_size=batch_size) 1155 1156 # Prepare validation data. c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 619 feed_output_shapes, 620 check_batch_axis=False, # Don't enforce the batch size. --> 621 exception_prefix='target') 622 623 # Generate sample-wise weight values given the `sample_weight` and c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 143 ': expected ' + names[i] + ' to have shape ' + 144 str(shape) + ' but got array with shape ' + --> 145 str(data_shape)) 146 return data 147 ValueError: Error when checking target: expected dense_7 to have shape (1, 1) but got array with shape (1, 3) ```

如何提高LSTM拟合准确度

如图所示黄色是原始波形,蓝色是拟合出的曲线,该模型不管在训练集还是测试集上拟合程度都不高,只能拟合出大概趋势,无法拟合的更好, 数据集大小: 训练集:(85000, 50, 5) (85000,) 测试集(9000, 50, 5) (9000,) 最终loss: 0.0021 - val_loss: 0.0013,基本上无法再下降了 怎样才能提高精度使拟合度更高呢,想让拟合出来的结果和曲线本身尽可能接近 ``` inputs1 = Input(shape=(train_x.shape[1], train_x.shape[2])) lstm1,state_h,state_c = LSTM(64,return_sequences=True,return_state=True)(inputs1) lstm2 = LSTM(64)(lstm1) lstm2 = Dense(16)(lstm2) lstm2 = Dropout(0.5)(lstm2) lstm4 = Dense(1)(lstm2) model = Model(input = inputs1,outputs = [lstm4]) model.summary() model.compile(loss='mse', optimizer='adam',) history = model.fit(train_x, train_y, epochs=600, batch_size=256, validation_data=(test_x,test_y),verbose=2, shuffle=False) # validation_data=(test_x,test_y) ``` ![图片说明](https://img-ask.csdn.net/upload/202005/29/1590716450_762209.png)![图片说明](https://img-ask.csdn.net/upload/202005/29/1590716461_922655.png)

为什么对于稍微有点不同的数据集,LSTM时间序列预测的预测结果会非常不一样?

下面的蓝色是训练集sin函数,time steps=15,predict steps = 5 红色的是预测结果,绿色是真实值 ![图片说明](https://img-ask.csdn.net/upload/202006/07/1591499342_185457.jpg) ![图片说明](https://img-ask.csdn.net/upload/202006/07/1591499360_261349.jpg) 然而,当我稍微换了以下训练数据:下面的蓝色是训练集sin函数,同样的,time steps=15,predict steps = 5 红色的是预测结果,绿色是真实值。这时的预测就变得很有问题了,这是为什么呢?我只换了数据,其他什么都没变 ![图片说明](https://img-ask.csdn.net/upload/202006/07/1591499534_265829.jpg)![图片说明](https://img-ask.csdn.net/upload/202006/07/1591499543_275921.jpg)

lstm做时序预测一直过拟合该怎么解决

做的8推1的时序预测,但是不管怎么修改模型,一直都是过拟合的状态,loss下降 val loss上升,val loss一点下降趋势也没有,直接往上走, 已经用了dropout(0.5)也试过用BatchNormalization(),但都没办法完全改善 想知道到底问题出在哪里 训练集和测试集(x,y)大小是(80000, 10, 8) (25000, 10, 8) (80000,) (25000,) ``` #数据整理 train = values[0:timestep*num] valid =values[timestep*num:] time_stamp = 10 scaled_data = scaler.fit_transform(train) x_train, y_train = [], [] for i in range(time_stamp, len(train)): x_train.append(scaled_data[i - time_stamp:i,0:8]) y_train.append(scaled_data[i, 8]) # y_train.append(scaled_data[i - time_stamp, 8]) # y_train.append(scaled_data[i - time_stamp:i, 8]) x_train, y_train = np.array(x_train), np.array(y_train) scaled_data = scaler.fit_transform(valid) x_valid, y_valid = [], [] for i in range(time_stamp, len(valid)): x_valid.append(scaled_data[i - time_stamp:i,0:8]) y_valid.append(scaled_data[i, 8]) # y_valid.append(scaled_data[i - time_stamp, 8]) # y_valid.append(scaled_data[i - time_stamp:i, 8]) x_valid, y_valid = np.array(x_valid), np.array(y_valid) print(x_train.shape,x_valid.shape, y_train.shape, y_valid.shape) #lstm模型 epochs = 60 batch_size = 256 model = Sequential() model.add(LSTM(units=128, return_sequences=True, input_dim=x_train.shape[-1], input_length=x_train.shape[1])) model.add(Dropout(0.5)) model.add(LSTM(units=64)) model.add(Dropout(0.5)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='rmsprop') history =model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size,validation_data=(x_valid,y_valid), verbose=2) ``` ![图片说明](https://img-ask.csdn.net/upload/202004/21/1587482938_348380.png) 上图中蓝色为loss曲线,黄色为val loss

LSTM预测出现这种滞后性,请问如何解决?

![图片说明](https://img-ask.csdn.net/upload/202006/17/1592357500_803445.jpg) LSTM预测出现这种滞后性,具体的解决方法,求教!

请大佬告知keras lstm输出问题

``` ip = Input(shape=(1,10)) op = BatchNormalization()(ip) print(ip.shape) op = LSTM(10, return_sequences=True, dropout=0.1, #input_shape=(1, 193), recurrent_dropout=0.2)(op) print(op.shape) op = Flatten()(op) out = Dense(5,activation='softmax')(op) model = Model(ip,out) model.summary() model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) x = train_x1 kfold = StratifiedKFold(n_splits=10, shuffle=True) x = x.reshape(-1,1,10) cvscores = [] for train, test in kfold.split(x, y): print(x[train].shape,x[test].shape) model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) history = model.fit(x[train], y[train], epochs=300, batch_size=128) scores = model.evaluate(x[test], y[test], verbose=0) print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) cvscores.append(scores[1] * 100) ``` Layer (type) Output Shape Param # ================================================================= input_22 (InputLayer) (None, 1, 10) 0 _________________________________________________________________ batch_normalization_9 (Batch (None, 1, 10) 40 _________________________________________________________________ lstm_16 (LSTM) (None, 1, 10) 840 _________________________________________________________________ flatten_5 (Flatten) (None, 10) 0 _________________________________________________________________ dense_23 (Dense) (None, 5) 55 ================================================================= Total params: 935 Trainable params: 915 Non-trainable params: 20 我这是个语音模型,输入数据是几百条语音,每条语音有10个压缩特征。我reshape成(-1,1,10)输入到lstm里面,但是最后想做一个5分类。但是总报错 ValueError: Error when checking target: expected dense_23 to have shape (5,) but got array with shape (1,) 求问大佬这个lstm层输出后应该怎么做?还是我之前的步骤有问题?

使用keras搭的模型,训练时候,使用相同的loss和metrics,但是输出却不一样

keras搭的模型,训练时候,使用相同的loss和metrics,但是输出却不一样,为什么会出现这种情况呀

keras下用RNN中的lstm来进行图片分类,输入维数的错误

1.如题,我是在keras下用lstm来对本地文件夹中六类垃圾进行图片分类 这是我的部分代码: (我本地的图片是512 ✖384的,进行resize为200✖160了) ``` nb_lstm_outputs = 128 #神经元个数 nb_time_steps = 200 #时间序列长度 nb_input_vector = 160 #输入序列 # 读取数据和标签 print("------开始读取数据------") data = [] labels = [] # 拿到图像数据路径,方便后续读取 imagePaths = sorted(list(utils_paths.list_images('./dataset-resized'))) random.seed(42) random.shuffle(imagePaths) # 遍历读取数据 for imagePath in imagePaths: # 读取图像数据 image = cv2.imread(imagePath) image = cv2.resize(image, (160,200)) data.append(image) # 读取标签 label = imagePath.split(os.path.sep)[-2] labels.append(label) # 对图像数据做scale操作 data=np.array(data, dtype="float") / 255.0 labels = np.array(labels) # 数据集切分 (trainX, testX, trainY, testY) = train_test_split(data,labels, test_size=0.25, random_state=42) # 转换标签为one-hot encoding格式 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # 设置初始化超参数 EPOCHS = 5 BS = 71 ``` 以上就是我的数据预处理操作 下面是我构建的模型: ``` model = Sequential() model.add(LSTM(units=nb_lstm_outputs, return_sequences=True, input_shape=(nb_time_steps, nb_input_vector))) # returns a sequence of vectors of dimension 30 model.add(LSTM(units=nb_lstm_outputs, return_sequences=True)) # returns a sequence of vectors of dimension 30 model.add(LSTM(units=nb_lstm_outputs)) # return a single vector of dimension 30 model.add(Dense(1, activation='softmax')) model.add(Dense(6, activation='softmax')) adam=Adam(lr=1e-4) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(trainX, trainY, epochs = EPOCHS, batch_size = BS, verbose = 1, validation_data = (testX,testY)) ``` 后续就是优化和生成loss等的代码了。 然而运行时遇到了以下维度错误: ![图片说明](https://img-ask.csdn.net/upload/202004/26/1587884348_141131.png) 然后我我试着修改不同的尺寸,发现都有上述错误,感觉应该是维度错误,但是不太明白1895是怎么来的? 2.遇到上述维度问题后,不太清楚怎么解决,于是我将代码中读取图片cv2.imread,将图像进行了灰度化: ``` image = cv2.imread(imagePath,CV2.IMREAD_GRAYSCALE) ``` 调整后,代码可以运行,然而并未按照预先设定的Batchsize进行训练,而是直接以划分的整体比例进行训练,想请问下这是怎么回事?已经输入BS到batch_size的参数了 ![图片说明](https://img-ask.csdn.net/upload/202004/26/1587884791_796238.png) 所以想请问各位大神,怎么解决维度问题,还有就是为什么后面BS传进去不管用啊,有没有清楚怎么一回事的啊? 谢谢各位大神了!!是个小白QAQ谢谢!

tensorflow训练完模型直接测试和导入模型进行测试的结果不同,一个很好,一个略差,这是为什么?

在tensorflow训练完模型,我直接采用同一个session进行测试,得到结果较好,但是采用训练完保存的模型,进行重新载入进行测试,结果较差,不懂是为什么会出现这样的结果。注:测试数据是一样的。以下是模型结果: 训练集:loss:0.384,acc:0.931. 验证集:loss:0.212,acc:0.968. 训练完在同一session内的测试集:acc:0.96。导入保存的模型进行测试:acc:0.29 ``` def create_model(hps): global_step = tf.Variable(tf.zeros([], tf.float64), name = 'global_step', trainable = False) scale = 1.0 / math.sqrt(hps.num_embedding_size + hps.num_lstm_nodes[-1]) / 3.0 print(type(scale)) gru_init = tf.random_normal_initializer(-scale, scale) with tf.variable_scope('Bi_GRU_nn', initializer = gru_init): for i in range(hps.num_lstm_layers): cell_bw = tf.contrib.rnn.GRUCell(hps.num_lstm_nodes[i], activation = tf.nn.relu, name = 'cell-bw') cell_bw = tf.contrib.rnn.DropoutWrapper(cell_bw, output_keep_prob = dropout_keep_prob) cell_fw = tf.contrib.rnn.GRUCell(hps.num_lstm_nodes[i], activation = tf.nn.relu, name = 'cell-fw') cell_fw = tf.contrib.rnn.DropoutWrapper(cell_fw, output_keep_prob = dropout_keep_prob) rnn_outputs, _ = tf.nn.bidirectional_dynamic_rnn(cell_bw, cell_fw, inputs, dtype=tf.float32) embeddedWords = tf.concat(rnn_outputs, 2) finalOutput = embeddedWords[:, -1, :] outputSize = hps.num_lstm_nodes[-1] * 2 # 因为是双向LSTM,最终的输出值是fw和bw的拼接,因此要乘以2 last = tf.reshape(finalOutput, [-1, outputSize]) # reshape成全连接层的输入维度 last = tf.layers.batch_normalization(last, training = is_training) fc_init = tf.uniform_unit_scaling_initializer(factor = 1.0) with tf.variable_scope('fc', initializer = fc_init): fc1 = tf.layers.dense(last, hps.num_fc_nodes, name = 'fc1') fc1_batch_normalization = tf.layers.batch_normalization(fc1, training = is_training) fc_activation = tf.nn.relu(fc1_batch_normalization) logits = tf.layers.dense(fc_activation, hps.num_classes, name = 'fc2') with tf.name_scope('metrics'): softmax_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits, labels = tf.argmax(outputs, 1)) loss = tf.reduce_mean(softmax_loss) # [0, 1, 5, 4, 2] ->argmax:2 因为在第二个位置上是最大的 y_pred = tf.argmax(tf.nn.softmax(logits), 1, output_type = tf.int64, name = 'y_pred') # 计算准确率,看看算对多少个 correct_pred = tf.equal(tf.argmax(outputs, 1), y_pred) # tf.cast 将数据转换成 tf.float32 类型 accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) with tf.name_scope('train_op'): tvar = tf.trainable_variables() for var in tvar: print('variable name: %s' % (var.name)) grads, _ = tf.clip_by_global_norm(tf.gradients(loss, tvar), hps.clip_lstm_grads) optimizer = tf.train.AdamOptimizer(hps.learning_rate) train_op = optimizer.apply_gradients(zip(grads, tvar), global_step) # return((inputs, outputs, is_training), (loss, accuracy, y_pred), (train_op, global_step)) return((inputs, outputs), (loss, accuracy, y_pred), (train_op, global_step)) placeholders, metrics, others = create_model(hps) content, labels = placeholders loss, accuracy, y_pred = metrics train_op, global_step = others def val_steps(sess, x_batch, y_batch, writer = None): loss_val, accuracy_val = sess.run([loss,accuracy], feed_dict = {inputs: x_batch, outputs: y_batch, is_training: hps.val_is_training, dropout_keep_prob: 1.0}) return loss_val, accuracy_val loss_summary = tf.summary.scalar('loss', loss) accuracy_summary = tf.summary.scalar('accuracy', accuracy) # 将所有的变量都集合起来 merged_summary = tf.summary.merge_all() # 用于test测试的summary merged_summary_test = tf.summary.merge([loss_summary, accuracy_summary]) LOG_DIR = '.' run_label = 'run_Bi-GRU_Dropout_tensorboard' run_dir = os.path.join(LOG_DIR, run_label) if not os.path.exists(run_dir): os.makedirs(run_dir) train_log_dir = os.path.join(run_dir, timestamp, 'train') test_los_dir = os.path.join(run_dir, timestamp, 'test') if not os.path.exists(train_log_dir): os.makedirs(train_log_dir) if not os.path.join(test_los_dir): os.makedirs(test_los_dir) # saver得到的文件句柄,可以将文件训练的快照保存到文件夹中去 saver = tf.train.Saver(tf.global_variables(), max_to_keep = 5) # train 代码 init_op = tf.global_variables_initializer() train_keep_prob_value = 0.2 test_keep_prob_value = 1.0 # 由于如果按照每一步都去计算的话,会很慢,所以我们规定每100次存储一次 output_summary_every_steps = 100 num_train_steps = 1000 # 每隔多少次保存一次 output_model_every_steps = 500 # 测试集测试 test_model_all_steps = 4000 i = 0 session_conf = tf.ConfigProto( gpu_options = tf.GPUOptions(allow_growth=True), allow_soft_placement = True, log_device_placement = False) with tf.Session(config = session_conf) as sess: sess.run(init_op) # 将训练过程中,将loss,accuracy写入文件里,后面是目录和计算图,如果想要在tensorboard中显示计算图,就想sess.graph加上 train_writer = tf.summary.FileWriter(train_log_dir, sess.graph) # 同样将测试的结果保存到tensorboard中,没有计算图 test_writer = tf.summary.FileWriter(test_los_dir) batches = batch_iter(list(zip(x_train, y_train)), hps.batch_size, hps.num_epochs) for batch in batches: train_x, train_y = zip(*batch) eval_ops = [loss, accuracy, train_op, global_step] should_out_summary = ((i + 1) % output_summary_every_steps == 0) if should_out_summary: eval_ops.append(merged_summary) # 那三个占位符输进去 # 计算loss, accuracy, train_op, global_step的图 eval_ops.append(merged_summary) outputs_train = sess.run(eval_ops, feed_dict={ inputs: train_x, outputs: train_y, dropout_keep_prob: train_keep_prob_value, is_training: hps.train_is_training }) loss_train, accuracy_train = outputs_train[0:2] if should_out_summary: # 由于我们想在100steps之后计算summary,所以上面 should_out_summary = ((i + 1) % output_summary_every_steps == 0)成立, # 即为真True,那么我们将训练的内容放入eval_ops的最后面了,因此,我们想获得summary的结果得在eval_ops_results的最后一个 train_summary_str = outputs_train[-1] # 将获得的结果写训练tensorboard文件夹中,由于训练从0开始,所以这里加上1,表示第几步的训练 train_writer.add_summary(train_summary_str, i + 1) test_summary_str = sess.run([merged_summary_test], feed_dict = {inputs: x_dev, outputs: y_dev, dropout_keep_prob: 1.0, is_training: hps.val_is_training })[0] test_writer.add_summary(test_summary_str, i + 1) current_step = tf.train.global_step(sess, global_step) if (i + 1) % 100 == 0: print("Step: %5d, loss: %3.3f, accuracy: %3.3f" % (i + 1, loss_train, accuracy_train)) # 500个batch校验一次 if (i + 1) % 500 == 0: loss_eval, accuracy_eval = val_steps(sess, x_dev, y_dev) print("Step: %5d, val_loss: %3.3f, val_accuracy: %3.3f" % (i + 1, loss_eval, accuracy_eval)) if (i + 1) % output_model_every_steps == 0: path = saver.save(sess,os.path.join(out_dir, 'ckp-%05d' % (i + 1))) print("Saved model checkpoint to {}\n".format(path)) print('model saved to ckp-%05d' % (i + 1)) if (i + 1) % test_model_all_steps == 0: # test_loss, test_acc, all_predictions= sess.run([loss, accuracy, y_pred], feed_dict = {inputs: x_test, outputs: y_test, dropout_keep_prob: 1.0}) test_loss, test_acc, all_predictions= sess.run([loss, accuracy, y_pred], feed_dict = {inputs: x_test, outputs: y_test, is_training: hps.val_is_training, dropout_keep_prob: 1.0}) print("test_loss: %3.3f, test_acc: %3.3d" % (test_loss, test_acc)) batches = batch_iter(list(x_test), 128, 1, shuffle=False) # Collect the predictions here all_predictions = [] for x_test_batch in batches: batch_predictions = sess.run(y_pred, {inputs: x_test_batch, is_training: hps.val_is_training, dropout_keep_prob: 1.0}) all_predictions = np.concatenate([all_predictions, batch_predictions]) correct_predictions = float(sum(all_predictions == y.flatten())) print("Total number of test examples: {}".format(len(y_test))) print("Accuracy: {:g}".format(correct_predictions/float(len(y_test)))) test_y = y_test.argmax(axis = 1) #生成混淆矩阵 conf_mat = confusion_matrix(test_y, all_predictions) fig, ax = plt.subplots(figsize = (4,2)) sns.heatmap(conf_mat, annot=True, fmt = 'd', xticklabels = cat_id_df.category_id.values, yticklabels = cat_id_df.category_id.values) font_set = FontProperties(fname = r"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", size=15) plt.ylabel(u'实际结果',fontsize = 18,fontproperties = font_set) plt.xlabel(u'预测结果',fontsize = 18,fontproperties = font_set) plt.savefig('./test.png') print('accuracy %s' % accuracy_score(all_predictions, test_y)) print(classification_report(test_y, all_predictions,target_names = cat_id_df['category_name'].values)) print(classification_report(test_y, all_predictions)) i += 1 ``` 以上的模型代码,请求各位大神帮我看看,为什么出现这样的结果?

多输入的时间序列预测有哪些办法?

我需要使用MATLAB做一个多输入的客流预测模型,输入为一个**t行n列**的矩阵(t为时间,n为站点数),前90%作为训练集,后10%作为测试集。 现在我可以做到输入为**t行1列**的矩阵,使用的是**深度学习的LSTM网络**,可以达到很好的预测效果; 其次我还了解到,使用**NARX神经网络**可以实现**t行2列**的预测模型(还没有实现)。我想知道,有什么方法可以实现**t行n列**的预测?或者可以对LSTM网络或NARX网络进行怎样的设置达到预测t行n列的目的? 感谢各位不吝赐教

最近在NLP领域做研究,应用十折交叉验证时遇到以下疑惑,求大佬们解惑?

1. 当有训练集、验证集和测试集的时候,可以通过验证集来监控模型是否发生过拟合,以及时停止训练,而测试集是用来评估最终模型泛化能力。当使用十折交叉验证后,九成的数据用作训练集,一成的数据用作验证集,在训练的过程中,是不是当验证集的准确率收敛后就可以停止训练呢?(我现在的做法就是这样的) 2. 当模型应用十折交叉验证跑完后,会得到10个不同验证集的准确率。我是最后计算不同模型的10个验证集的平均准确率,选择平均准确率最高的模型作为最终模型,这样可以吗?(我看网上有的说是选择具有最小泛化误差的模型作为最终模型,不知道是应该选择最小泛化误差还是最大平均准确率) 3. 当确定最终模型后,使用全部数据作为训练集进行训练,那么在没有验证集的情况下,如何确定epoch使得模型结果不会过拟合呢?是在每次epoch训练后,通过训练集本身的准确率是否已经收敛来及时停下吗(我感觉这样做比较容易过拟合,因此我现在的做法是在刚好超过 使用十折交叉验证时验证集的平均准确率 的时候就停了下来)? 4. 对我们的原始数据集进行了四种不同的处理(或者说划分):random、expert、bundle和table,处理后使用十折交叉验证,发现random方法下,在验证集准确率收敛后,其准确率有0.88左右,训练集的准确率可以达到0.96以上,这是不是算是过拟合呢? <br>使用同样的超参数(如learning rate,epoch,batch size,dropout rate等都一样),在expert等其他三种划分下,在验证集准确率收敛后,其准确率只有0.68左右,但训练集的准确率依旧可达0.96以上,这是明显的过拟合吗?0.68的准确率能反应这个数据集在这种情况下划分的泛化能力吗?

如何防止过拟合?为何我的训练准确率高,但是测试准确率很低?

问题如标题 print('Training ------------') # training the model model.fit(X_train, y_train, epochs=8, batch_size=32,) Epoch 1/8 19578/19578 [==============================] - 334s 17ms/step - loss: 1.9936 - acc: 0.3272 Epoch 2/8 19578/19578 [==============================] - 325s 17ms/step - loss: 1.3145 - acc: 0.5698 Epoch 3/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.9667 - acc: 0.6897 Epoch 4/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.7580 - acc: 0.7557 Epoch 5/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.5882 - acc: 0.8095 Epoch 6/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.4548 - acc: 0.8510 Epoch 7/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.3471 - acc: 0.8839 Epoch 8/8 19578/19578 [==============================] - 325s 17ms/step - loss: 0.2524 - acc: 0.9176 print('\nTesting ------------') # Evaluate the model with the metrics we defined earlier loss, accuracy = model.evaluate(X_test1, y_test1) print('\ntest loss: ', loss) print('\ntest accuracy: ', accuracy) Testing ------------ 3000/3000 [==============================] - 16s 5ms/step test loss: 15.392780853271484 test accuracy: 0.045

R语言 三分类模型问题

R语言里一般的二项分类问题建立数据模型我们可以用glm函数,但是如果是三分类的问题(足球胜平负 与其他变量的关系)R语言里有没有什么包里面的function可以直接用的呀?

训练准确率很高,测试准确率很低(loss有一直下降)

tensorflow模型,4层cnn 跑出来的结果训练集能达到1,测试集准确率基本保持在0.5左右,二分类 数据有用shuffle, 有dropout,有正则化,learning rate也挺小的。 不知道是什么原因,求解答! ![图片说明](https://img-ask.csdn.net/upload/201912/11/1576068416_852077.png)

pytorch自定义初始化权重后模型loss一直在2点几

``` class Net(nn.Module): def __init__(self): super(Net,self).__init__() self.conv1 = nn.Conv2d(3,64,3,padding=1,bias=False) self.conv2 = nn.Conv2d(64,64,3,padding=1,bias=False) self.pool1 = nn.MaxPool2d(2, 2) self.bn1 = nn.BatchNorm2d(64) self.relu1 = nn.ReLU() self.conv3 = nn.Conv2d(64,128,3,padding=1,bias=False) self.conv4 = nn.Conv2d(128, 128, 3,padding=1,bias=False) self.pool2 = nn.MaxPool2d(2, 2, padding=1) self.bn2 = nn.BatchNorm2d(128) self.relu2 = nn.ReLU() self.conv5 = nn.Conv2d(128,128, 3,padding=1,bias=False) self.conv6 = nn.Conv2d(128, 128, 3,padding=1,bias=False) self.conv7 = nn.Conv2d(128, 128, 1,padding=1,bias=False) self.pool3 = nn.MaxPool2d(2, 2, padding=1) self.bn3 = nn.BatchNorm2d(128) self.relu3 = nn.ReLU() self.conv8 = nn.Conv2d(128, 256, 3,padding=1,bias=False) self.conv9 = nn.Conv2d(256, 256, 3, padding=1,bias=False) self.conv10 = nn.Conv2d(256, 256, 1, padding=1,bias=False) self.pool4 = nn.MaxPool2d(2, 2, padding=1) self.bn4 = nn.BatchNorm2d(256) self.relu4 = nn.ReLU() self.conv11 = nn.Conv2d(256, 512, 3, padding=1,bias=False) self.conv12 = nn.Conv2d(512, 512, 3, padding=1,bias=False) self.conv13 = nn.Conv2d(512, 512, 1, padding=1,bias=False) self.pool5 = nn.MaxPool2d(2, 2, padding=1) self.bn5 = nn.BatchNorm2d(512) self.relu5 = nn.ReLU() self.fc14 = nn.Linear(512*4*4,1024) self.drop1 = nn.Dropout2d() self.fc15 = nn.Linear(1024,1024) self.drop2 = nn.Dropout2d() self.fc16 = nn.Linear(1024,10) def forward(self,x): x = self.conv1(x) x = self.conv2(x) x = self.pool1(x) x = self.bn1(x) x = self.relu1(x) x = self.conv3(x) x = self.conv4(x) x = self.pool2(x) x = self.bn2(x) x = self.relu2(x) x = self.conv5(x) x = self.conv6(x) x = self.conv7(x) x = self.pool3(x) x = self.bn3(x) x = self.relu3(x) x = self.conv8(x) x = self.conv9(x) x = self.conv10(x) x = self.pool4(x) x = self.bn4(x) x = self.relu4(x) x = self.conv11(x) x = self.conv12(x) x = self.conv13(x) x = self.pool5(x) x = self.bn5(x) x = self.relu5(x) # print(" x shape ",x.size()) x = x.view(-1,512*4*4) x = F.relu(self.fc14(x)) x = self.drop1(x) x = F.relu(self.fc15(x)) x = self.drop2(x) x = self.fc16(x) return x model = Net() #model = torch.nn.DataParallel(model) if t.cuda.is_available(): model.cuda() print(model) # torchvision输出的是PILImage,值的范围是[0, 1]. # 我们将其转化为tensor数据,并归一化为[-1, 1]。 transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) # 训练集,将相对目录./data下的cifar-10-batches-py文件夹中的全部数据(50000张图片作为训练数据)加载到内存中,若download为True时,会自动从网上下载数据并解压 trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform) # 将训练集的50000张图片划分成12500份,每份4张图,用于mini-batch输入。shffule=True在表示不同批次的数据遍历时,打乱顺序。num_workers=2表示使用两个子进程来加载数据 trainloader = torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=False, num_workers=1) #测试集,将相对目录./data下的cifar-10-batches-py文件夹中的全部数据(10000张图片作为测试数据)加载到内存中,若download为True时,会自动从网上下载数据并解压 testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform) # 将测试集的10000张图片划分成2500份,每份4张图,用于mini-batch输入。 testloader = torch.utils.data.DataLoader(testset, batch_size=50, shuffle=False, num_workers=1) criterion = nn.CrossEntropyLoss()#叉熵损失函数 optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)#使用SGD(随机梯度下降)优化,学习率为0.001,动量为0.9 for epoch in range(1): # 遍历数据集10次 running_loss = 0.0 # enumerate(sequence, [start=0]),i序号,data是数据 for i, data in enumerate(trainloader, 0): #for i in range(1000): # get the inputs #data is list inputs, labels = data # data的结构是:[4x3x32x32的张量,长度4的张量] # wrap them in Variable inputs = Variable(inputs) labels=Variable(labels)# 把input数据从tensor转为variable if t.cuda.is_available(): inputs=inputs.cuda() labels=labels.cuda() #inputs,labels= inputs.type(torch.FloatTensor),labels.type(torch.FloatTensor) # zero the parameter gradients optimizer.zero_grad() # 将参数的grad值初始化为0 # forward + backward + optimize outputs = model(inputs) loss = criterion(outputs, labels) # 将output和labels使用叉熵计算损失 loss.backward() # 反向传播 optimizer.step() # 用SGD更新参数 # 每2000批数据打印一次平均loss值 running_loss += loss.item() # loss本身为Variable类型,所以要使用data获取其Tensor,因为其为标量,所以取0 或使用loss.item() if i % 500 == 499: # 每2000批打印一次 print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 500)) running_loss = 0.0 print('Finished Training') start = time.clock()#.time correct = 0 total = 0 for data in testloader: images, labels = data images=images.cuda() labels=labels.cuda() outputs = model(Variable(images)) print(outputs.shape) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) end = time.clock()#.time print("Running time: %s Seconds" % (end-start)) weight0=np.random.random((64,3,3,3)) weight0=nn.Parameter(torch.FloatTensor(weight0).cuda(),requires_grad=True) #weight1=np.random.random((64,64,3,3),dtype=np.float32) weight1=np.random.random((64,64,3,3)) weight1=nn.Parameter(torch.FloatTensor(weight1).cuda(),requires_grad=True) weight2=np.random.random((128,64,3,3)) weight2=nn.Parameter(torch.FloatTensor(weight2).cuda(),requires_grad=True) weight3=np.random.random((128,128,3,3)) weight3=nn.Parameter(torch.FloatTensor(weight3).cuda(),requires_grad=True) weight4=np.random.random((128,128,3,3)) weight4=nn.Parameter(torch.FloatTensor(weight4).cuda(),requires_grad=True) weight5=np.random.random((128,128,3,3)) weight5=nn.Parameter(torch.FloatTensor(weight5).cuda(),requires_grad=True) weight6=np.random.random((128,128,1,1)) weight6=nn.Parameter(torch.FloatTensor(weight6).cuda(),requires_grad=True) weight7=np.random.random((256,128,3,3)) weight7=nn.Parameter(torch.FloatTensor(weight7).cuda(),requires_grad=True) weight8=np.random.random((256,256,3,3)) weight8=nn.Parameter(torch.FloatTensor(weight8).cuda(),requires_grad=True) weight9=np.random.random((256,256,1,1)) weight9=nn.Parameter(torch.FloatTensor(weight9).cuda(),requires_grad=True) weight10=np.random.random((512,256,3,3)) weight10=nn.Parameter(torch.FloatTensor(weight10).cuda(),requires_grad=True) weight11=np.random.random((512,512,3,3)) weight11=nn.Parameter(torch.FloatTensor(weight11).cuda(),requires_grad=True) weight12=np.random.random((512,512,1,1)) weight12=nn.Parameter(torch.FloatTensor(weight12).cuda(),requires_grad=True) new_layer_id=0 for m1 in model.modules(): if isinstance(m1, nn.Conv2d): if (new_layer_id==0): m1.weight = weight0 elif(new_layer_id==1): m1.weight= weight1 elif(new_layer_id==2): m1.weight = weight2 elif(new_layer_id==3): m1.weight = weight3 elif(new_layer_id==4): m1.weight = weight4 elif(new_layer_id==5): m1.weight = weight5 elif(new_layer_id==6): m1.weight = weight6 elif(new_layer_id==7): m1.weight = weight7 elif(new_layer_id==8): m1.weight = weight8 elif(new_layer_id==9): m1.weight = weight9 elif(new_layer_id==10): m1.weight = weight10 elif(new_layer_id==11): m1.weight = weight11 elif(new_layer_id==12): m1.weight = weight12 new_layer_id=new_layer_id+1 elif isinstance(m1, nn.BatchNorm2d): m1.weight = m1.weight m1.bias = m1.bias elif isinstance(m1, nn.Linear): m1.weight = m1.weight m1.bias = m1.bias #torchvision输出的是PILImage,值的范围是[0, 1]. # 我们将其转化为tensor数据,并归一化为[-1, 1]。 transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) # 训练集,将相对目录./data下的cifar-10-batches-py文件夹中的全部数据(50000张图片作为训练数据)加载到内存中,若download为True时,会自动从网上下载数据并解压 trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform) # 将训练集的50000张图片划分成12500份,每份4张图,用于mini-batch输入。shffule=True在表示不同批次的数据遍历时,打乱顺序。num_workers=2表示使用两个子进程来加载数据 trainloader = torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=False, num_workers=1) #测试集,将相对目录./data下的cifar-10-batches-py文件夹中的全部数据(10000张图片作为测试数据)加载到内存中,若download为True时,会自动从网上下载数据并解压 testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform) # 将测试集的10000张图片划分成2500份,每份4张图,用于mini-batch输入。 testloader = torch.utils.data.DataLoader(testset, batch_size=50, shuffle=False, num_workers=1) criterion1 = nn.CrossEntropyLoss()#叉熵损失函数 optimizer1 = optim.Adam(model.parameters(), lr=0.001)#使用SGD(随机梯度下降)优化,学习率为0.001,动量为0.9 #momentum=0.9 start = time.clock()#.time correct = 0 total = 0 for data in testloader: images, labels = data images=images.cuda() labels=labels.cuda() outputs = model(Variable(images)) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum() print('Accuracy of the Newmodel1 on the 10000 test images: %d %%' % ( 100 * correct / total)) end = time.clock()#.time print("Running time: %s Seconds" % (end-start)) for epoch in range(20): running_loss = 0.0 for i, data in enumerate(trainloader, 0): #for i in range(1000): inputs, labels = data inputs = Variable(inputs) #print(inputs.shape) labels=Variable(labels) inputs=inputs.cuda() labels=labels.cuda() optimizer1.zero_grad() outputs = model(inputs) loss = criterion1(outputs, labels) loss.backward() optimizer1.step() running_loss += loss.item() if i % 500 == 499: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 500)) ```

为什么训练一个简单的残差网络时,loss并不低,但是acc还可以

使用一个20层左右的一维卷积神经网络来对一组机械振动数据分类, loss训练过50epochs还是0.9,但正确率90%出头,在测试集上也差不多 请问一下是为什么

tensorflow中相同的代码,运行多次每次的结果都不相同?

本人渣渣一枚,最近在tensorflow中遇到以下问题: 先上一下代码, ![图片说明](https://img-ask.csdn.net/upload/201905/08/1557330204_410400.png) 图片1,加载文件中的参数,并将其值赋给常量(避免后期值被改变,渣渣只会这种操作,哭) ![图片说明](https://img-ask.csdn.net/upload/201905/08/1557330557_60385.png) 图片2,加载训练数据集 ![图片说明](https://img-ask.csdn.net/upload/201905/08/1557330595_932800.png) 图片3,也就是有问题的地方。 具体问题如下:当我执行完图片3的代码后,再执行一次,结果就和第一次不一样,必须要在执行图片3之前再执行一次图片1才能使结果一致,见下图: ![图片说明](https://img-ask.csdn.net/upload/201905/09/1557331809_938473.png) ![图片说明](https://img-ask.csdn.net/upload/201905/09/1557331870_36226.png) 求指导,感谢!!!!! 附上全部代码: # coding: utf-8 # In[1]: import tensorflow as tf import matplotlib.pyplot as plt # In[2]: import Ipynb_importer # In[3]: from core3 import * # In[5]: with tf.Session() as sess: ww,bb=load_weights('e:/python/savedata/','keep4',3) w00=ww[0] w01=ww[1] w02=ww[2] w03=ww[3] b00=bb[0] b01=bb[1] b02=bb[2] b03=bb[3] sess.run(tf.global_variables_initializer()) w10=w00.eval() w11=w01.eval() w12=w02.eval() w13=w03.eval() b10=b00.eval() b11=b01.eval() b12=b02.eval() b13=b03.eval() w0=tf.constant(w10) w1=tf.constant(w11) w2=tf.constant(w12) w3=tf.constant(w13) b0=tf.constant(b10) b1=tf.constant(b11) b2=tf.constant(b12) b3=tf.constant(b13) # In[8]: X_train, y_train, X_test, y_test, train_x_mean, train_x_std,train_y_mean0,train_y_std=get_data('e:/Python/jupyter/fuxian/data/2_layer_tio2',percentTest=.2,random_state=42) print(y_train) print(X_train) # In[ ]: with tf.Session() as sess: x0=tf.convert_to_tensor(X_train) x1=tf.cast(x0,tf.float32) a1 = tf.sigmoid(tf.add(tf.matmul(x1, w0),b0)) b1 = tf.sigmoid(tf.add(tf.matmul(a1, w1),b1)) c1 = tf.sigmoid(tf.add(tf.matmul(b1, w2),b2)) y1p = tf.add(tf.matmul(c1, w3),b3) y1=y1p.eval() m=tf.reduce_mean(np.abs((y1-y_train)/y_train))*100 print(sess.run(m)) 补充: ![图片说明](https://img-ask.csdn.net/upload/201905/09/1557369185_458961.png) 之后又尝试通过上图的代码一步一步找问题,在第一次执行和第二次执行的结果中,a1的值相同,b1的值不相同,但w0,w1的值是相同的,这又是为什么?求指导!!感谢!!

为什么同样的问题用Tensorflow和keras实现结果不一样?

**cifar-10分类问题,同样的模型结构以及损失函数还有学习率参数等超参数,分别用TensorFlow和keras实现。 20个epochs后在测试集上进行预测,准确率总是差好几个百分点,不知道问题出在哪里?代码如下: 这个是TF的代码:** import tensorflow as tf import numpy as np import pickle as pk tf.reset_default_graph() batch_size = 64 test_size = 10000 img_size = 32 num_classes = 10 training_epochs = 10 test_size=200 ############################################################################### def unpickle(filename): '''解压数据''' with open(filename, 'rb') as f: d = pk.load(f, encoding='latin1') return d def onehot(labels): '''one-hot 编码''' n_sample = len(labels) n_class = max(labels) + 1 onehot_labels = np.zeros((n_sample, n_class)) onehot_labels[np.arange(n_sample), labels] = 1 return onehot_labels # 训练数据集 data1 = unpickle('data_batch_1') data2 = unpickle('data_batch_2') data3 = unpickle('data_batch_3') data4 = unpickle('data_batch_4') data5 = unpickle('data_batch_5') X_train = np.concatenate((data1['data'], data2['data'], data3['data'], data4['data'], data5['data']), axis=0)/255.0 y_train = np.concatenate((data1['labels'], data2['labels'], data3['labels'], data4['labels'], data5['labels']), axis=0) y_train = onehot(y_train) # 测试数据集 test = unpickle('test_batch') X_test = test['data']/255.0 y_test = onehot(test['labels']) del test,data1,data2,data3,data4,data5 ############################################################################### w = tf.Variable(tf.random_normal([5, 5, 3, 32], stddev=0.01)) w_c= tf.Variable(tf.random_normal([32* 16* 16, 512], stddev=0.1)) w_o =tf.Variable(tf.random_normal([512, num_classes], stddev=0.1)) def init_bias(shape): return tf.Variable(tf.constant(0.0, shape=shape)) b=init_bias([32]) b_c=init_bias([512]) b_o=init_bias([10]) def model(X, w, w_c,w_o, p_keep_conv, p_keep_hidden,b,b_c,b_o): conv1 = tf.nn.conv2d(X, w,strides=[1, 1, 1, 1],padding='SAME')#32x32x32 conv1=tf.nn.bias_add(conv1,b) conv1 = tf.nn.relu(conv1) conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='SAME')#16x16x32 conv1 = tf.nn.dropout(conv1, p_keep_conv) FC_layer = tf.reshape(conv1, [-1, 32 * 16 * 16]) out_layer=tf.matmul(FC_layer, w_c)+b_c out_layer=tf.nn.relu(out_layer) out_layer = tf.nn.dropout(out_layer, p_keep_hidden) result = tf.matmul(out_layer, w_o)+b_o return result trX, trY, teX, teY = X_train,y_train,X_test,y_test trX = trX.reshape(-1, img_size, img_size, 3) teX = teX.reshape(-1, img_size, img_size, 3) X = tf.placeholder("float", [None, img_size, img_size, 3]) Y = tf.placeholder("float", [None, num_classes]) p_keep_conv = tf.placeholder("float") p_keep_hidden = tf.placeholder("float") py_x = model(X, w, w_c,w_o, p_keep_conv, p_keep_hidden,b,b_c,b_o) Y_ = tf.nn.softmax_cross_entropy_with_logits_v2(logits=py_x, labels=Y) cost = tf.reduce_mean(Y_) optimizer = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) predict_op = tf.argmax(py_x, 1) with tf.Session() as sess: tf.global_variables_initializer().run() for i in range(training_epochs): training_batch = zip(range(0, len(trX),batch_size),range(batch_size, len(trX)+1,batch_size)) perm=np.arange(len(trX)) np.random.shuffle(perm) trX=trX[perm] trY=trY[perm] for start, end in training_batch: sess.run(optimizer, feed_dict={X: trX[start:end],Y: trY[start:end],p_keep_conv:0.75,p_keep_hidden: 0.5}) test_batch = zip(range(0, len(teX),test_size),range(test_size, len(teX)+1,test_size)) accuracyResult=0 for start, end in test_batch: accuracyResult=accuracyResult+sum(np.argmax(teY[start:end], axis=1) ==sess.run(predict_op, feed_dict={X: teX[start:end],Y: teY[start:end],p_keep_conv: 1,p_keep_hidden: 1})) print(i, accuracyResult/10000) **这个是keras代码:** from keras import initializers from keras.datasets import cifar10 from keras.utils import np_utils from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.optimizers import SGD, Adam, RMSprop #import matplotlib.pyplot as plt # CIFAR_10 is a set of 60K images 32x32 pixels on 3 channels IMG_CHANNELS = 3 IMG_ROWS = 32 IMG_COLS = 32 #constant BATCH_SIZE = 64 NB_EPOCH = 10 NB_CLASSES = 10 VERBOSE = 1 VALIDATION_SPLIT = 0 OPTIM = RMSprop() #load dataset (X_train, y_train), (X_test, y_test) = cifar10.load_data() #print('X_train shape:', X_train.shape) #print(X_train.shape[0], 'train samples') #print(X_test.shape[0], 'test samples') # convert to categorical Y_train = np_utils.to_categorical(y_train, NB_CLASSES) Y_test = np_utils.to_categorical(y_test, NB_CLASSES) # float and normalization X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 # network model = Sequential() model.add(Conv2D(32, (3, 3), padding='same',input_shape=(IMG_ROWS, IMG_COLS, IMG_CHANNELS),kernel_initializer=initializers.random_normal(stddev=0.01),bias_initializer=initializers.Zeros())) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) #0<参数<1才会有用 model.add(Flatten()) model.add(Dense(512,kernel_initializer=initializers.random_normal(stddev=0.1),bias_initializer=initializers.Zeros())) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(NB_CLASSES,kernel_initializer=initializers.random_normal(stddev=0.1),bias_initializer=initializers.Zeros())) model.add(Activation('softmax')) model.summary() # train model.compile(loss='categorical_crossentropy', optimizer=OPTIM,metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=BATCH_SIZE,epochs=NB_EPOCH, validation_split=VALIDATION_SPLIT,verbose=VERBOSE) score = model.evaluate(X_test, Y_test,batch_size=200, verbose=VERBOSE) print("Test score:", score[0]) print('Test accuracy:', score[1])

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

相关热词 c# 局部 截图 页面 c#实现简单的文件管理器 c# where c# 取文件夹路径 c# 对比 当天 c# fir 滤波器 c# 和站 队列 c# txt 去空格 c#移除其他类事件 c# 自动截屏
立即提问