deeplab v3+训练loss不收敛问题
python deeplab/train.py \
    --logtostderr \
    --training_number_of_steps=30000 \
    --train_split="train" \
    --model_variant="xception_65" \
    --atrous_rates=6 \
    --atrous_rates=12 \
    --atrous_rates=18 \
    --output_stride=16 \
    --decoder_output_stride=4 \
    --train_crop_size=513 \
    --train_crop_size=513 \
    --train_batch_size=2 \
    --dataset="pascal_voc_seg" \
    --fine_tune_batch_norm = False \
    --tf_initial_checkpoint="{下载的checkpoint路径}/deeplabv3_pascal_train_aug/model.ckpt.index" \
    --train_logdir="{要写入路径}/exp/train_on_train_set/train" \
    --dataset_dir="{数据集路径}/pascal_voc_seg/tfrecord"
  • 然而loss一直不收敛:图片说明
  • 最终出现nan值错误图片说明
  • 如果训练的次数少一点,验证一下结果,发现miou只有零点零几:图片说明

  • 一直没有找到原因,感觉步骤没有问题,也参照过各种博客,大家似乎都没有出现这种情况,希望大佬们可以帮忙

1个回答

解决了。模型导入错误,应该导入model.ckpt而不是model.ckpt.index。唉我居然一直没发现真是傻了

EmbarrassFatTiger
EmbarrassFatTiger 就是直接把那行命令中的.index删掉就行了吗?我看文件夹了里没有model.ckpt呀,新手求教
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
deeplab v3+训练loss不收敛问题
* 我使用的是官网的代码https://github.com/tensorflow/models/tree/master/research/deeplab 复现deeplab v3+; * 训练数据就是标准的Pascal voc2012。训练之前已经按照官网上的说法,通过运行脚本download_and_convert_voc2012.sh下载voc2012数据、并将label转换为单通道、并将数据转换为需要的tfrecord格式; * 训练模型也是从提供的model_zoo下载的https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/model_zoo.md; * 学习率保持默认,即learning rate=0.0001; * Linux Ubuntu 16.04;TensorFlow1.6.0 installed from Anaconda;CUDA9.0/cudnn7.0.5;GeForce GTX 1080 Ti; * 具体训练代码是: ``` python deeplab/train.py \ --logtostderr \ --training_number_of_steps=30000 \ --train_split="train" \ --model_variant="xception_65" \ --atrous_rates=6 \ --atrous_rates=12 \ --atrous_rates=18 \ --output_stride=16 \ --decoder_output_stride=4 \ --train_crop_size=513 \ --train_crop_size=513 \ --train_batch_size=2 \ --dataset="pascal_voc_seg" \ --fine_tune_batch_norm = False \ --tf_initial_checkpoint="{下载的checkpoint路径}/deeplabv3_pascal_train_aug/model.ckpt.index" \ --train_logdir="{要写入路径}/exp/train_on_train_set/train" \ --dataset_dir="{数据集路径}/pascal_voc_seg/tfrecord" ``` * 然而loss一直不收敛:![图片说明](https://img-ask.csdn.net/upload/201812/25/1545727654_85154.png) * 最终出现nan值错误![图片说明](https://img-ask.csdn.net/upload/201812/25/1545727689_745383.png) * 如果训练的次数少一点,验证一下结果,发现miou只有零点零几:![图片说明](https://img-ask.csdn.net/upload/201812/26/1545785941_997452.jpg) * 一直没有找到原因,感觉步骤没有问题,也参照过各种博客,大家似乎都没有出现这种情况,希望大佬们可以帮忙
Trace和Ping丢包率不同的问题
最近要开发一个小工具,学习一些网络检测定位的东西遇到了一个问题。我在第12跳上有一个IP地址。当我向其IP发送TTL为11的icmp请求数据包时,第11跳的数据包丢失为0%。 <br> 但是,如果我ping第11跳的IP地址,或使用TTL 11构建我的icmp请求数据包来trace第11跳,则数据包丢失会超过20%。 <br> 不太明白为什么。是路由器对reply包和time exceed包处理有区别吗?还是路由器向下一跳传递和回复源IP是两个不相关的东西。 <br> 还是需要像pathping一样,取得目的ip路由路径后用ping来统计丢包时延?不能一直对目的ip做trace来统计丢包时延? <br> 抱歉,由于某种原因我无法粘贴我的输出,但是我的输出看起来像这样。 ``` trace 123.123.123.124 IP TTL Loss xxx.xxx.xxx.xxx 1 0% xxx.xxx.xxx.xxx 2 0% xxx.xxx.xxx.xxx 3 0% xxx.xxx.xxx.xxx 4 0% xxx.xxx.xxx.xxx 5 0% xxx.xxx.xxx.xxx 6 0% xxx.xxx.xxx.xxx 7 0% xxx.xxx.xxx.xxx 8 0% xxx.xxx.xxx.xxx 9 0% xxx.xxx.xxx.xxx 10 0% 123.123.123.123 11 0% 123.123.123.124 12 20% trace 123.123.123.123 IP TTL Loss xxx.xxx.xxx.xxx 1 0% xxx.xxx.xxx.xxx 2 0% xxx.xxx.xxx.xxx 3 0% xxx.xxx.xxx.xxx 4 0% xxx.xxx.xxx.xxx 5 0% xxx.xxx.xxx.xxx 6 0% xxx.xxx.xxx.xxx 7 0% xxx.xxx.xxx.xxx 8 0% xxx.xxx.xxx.xxx 9 0% xxx.xxx.xxx.xxx 10 0% 123.123.123.123 11 20% ping 123.123.123.123 loss 20% ```
训练准确率很高,测试准确率很低(loss有一直下降)
tensorflow模型,4层cnn 跑出来的结果训练集能达到1,测试集准确率基本保持在0.5左右,二分类 数据有用shuffle, 有dropout,有正则化,learning rate也挺小的。 不知道是什么原因,求解答! ![图片说明](https://img-ask.csdn.net/upload/201912/11/1576068416_852077.png)
tensorflow 里loss 出现nan问题 新手问题
大家好, 新手刚刚学,IDE:spyder 预测一个停车场车辆的驶出率, 输入(时间和车辆驶入率)二维。 训练的数据就是,驶出率car/min,时间(时间:1代表一天,从凌晨10秒=10/24/3600的时候开始到晚上23点多),驶入率car/min, 只建了一层的hidden layer,然后print loss是都是nan... 不知道哪里出了问题,是因为层太简单了么?还是激活函数有问题呢? 看网上说排除零的影响,我把输入数和输出数都+1,变得非零了也还是nan... 代码如下: ``` import tensorflow as tf import numpy as np import pandas as pd data=pd.read_csv('0831new.csv') date=data['date'] erate=data['erate'] x=pd.concat([date,erate],axis=1) drate=data['drate'] y=np.array(drate) x=np.array(x) y=y.reshape([7112,1]) x=x+1 y=y+1 z=[] def add_layer(inputs, in_size, out_size, activation_function=None): # add one more layer and return the output of this layer Weights = tf.Variable(tf.random_normal([in_size, out_size])) biases = tf.Variable(tf.zeros([1, out_size]) + 0.01) Wx_plus_b = tf.matmul(inputs, Weights) + biases if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) return outputs xs = tf.placeholder(tf.float32, [None, 2]) ys = tf.placeholder(tf.float32, [None, 1]) l1 = add_layer(xs, 2, 5, activation_function=tf.nn.tanh) prediction = add_layer(l1,5, 1, activation_function=None) loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1,0])) train_step= tf.train.GradientDescentOptimizer(0.001).minimize(loss) if int((tf.__version__).split('.')[1]) < 12: init = tf.initialize_all_variables() else: init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for i in range(1000): # training sess.run(train_step, feed_dict={xs: x, ys: y}) if i % 25 == 0: # to see the step improvement print('loss:',sess.run(loss, feed_dict={xs:x, ys:y})) z.append(loss) ``` ![图片说明](https://img-ask.csdn.net/upload/201909/20/1568984044_999083.png) 帮忙给件建议吧~ 谢谢
tensorflow载入训练好的模型进行预测,同一张图片预测的结果却不一样????
最近在跑deeplabv1,在测试代码的时候,跑通了训练程序,但是用训练好的模型进行与测试却发现相同的图片预测的结果不一样??请问有大神知道怎么回事吗? 用的是saver.restore()方法载入模型。代码如下: ``` def main(): """Create the model and start the inference process.""" args = get_arguments() # Prepare image. img = tf.image.decode_jpeg(tf.read_file(args.img_path), channels=3) # Convert RGB to BGR. img_r, img_g, img_b = tf.split(value=img, num_or_size_splits=3, axis=2) img = tf.cast(tf.concat(axis=2, values=[img_b, img_g, img_r]), dtype=tf.float32) # Extract mean. img -= IMG_MEAN # Create network. net = DeepLabLFOVModel() # Which variables to load. trainable = tf.trainable_variables() # Predictions. pred = net.preds(tf.expand_dims(img, dim=0)) # Set up TF session and initialize variables. config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) #init = tf.global_variables_initializer() sess.run(tf.global_variables_initializer()) # Load weights. saver = tf.train.Saver(var_list=trainable) load(saver, sess, args.model_weights) # Perform inference. preds = sess.run([pred]) print(preds) if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) msk = decode_labels(np.array(preds)[0, 0, :, :, 0]) im = Image.fromarray(msk) im.save(args.save_dir + 'mask1.png') print('The output file has been saved to {}'.format( args.save_dir + 'mask.png')) if __name__ == '__main__': main() ``` 其中load是 ``` def load(saver, sess, ckpt_path): '''Load trained weights. Args: saver: TensorFlow saver object. sess: TensorFlow session. ckpt_path: path to checkpoint file with parameters. ''' ckpt = tf.train.get_checkpoint_state(ckpt_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("Restored model parameters from {}".format(ckpt_path)) ``` DeepLabLFOVMode类如下: ``` class DeepLabLFOVModel(object): """DeepLab-LargeFOV model with atrous convolution and bilinear upsampling. This class implements a multi-layer convolutional neural network for semantic image segmentation task. This is the same as the model described in this paper: https://arxiv.org/abs/1412.7062 - please look there for details. """ def __init__(self, weights_path=None): """Create the model. Args: weights_path: the path to the cpkt file with dictionary of weights from .caffemodel. """ self.variables = self._create_variables(weights_path) def _create_variables(self, weights_path): """Create all variables used by the network. This allows to share them between multiple calls to the loss function. Args: weights_path: the path to the ckpt file with dictionary of weights from .caffemodel. If none, initialise all variables randomly. Returns: A dictionary with all variables. """ var = list() index = 0 if weights_path is not None: with open(weights_path, "rb") as f: weights = cPickle.load(f) # Load pre-trained weights. for name, shape in net_skeleton: var.append(tf.Variable(weights[name], name=name)) del weights else: # Initialise all weights randomly with the Xavier scheme, # and # all biases to 0's. for name, shape in net_skeleton: if "/w" in name: # Weight filter. w = create_variable(name, list(shape)) var.append(w) else: b = create_bias_variable(name, list(shape)) var.append(b) return var def _create_network(self, input_batch, keep_prob): """Construct DeepLab-LargeFOV network. Args: input_batch: batch of pre-processed images. keep_prob: probability of keeping neurons intact. Returns: A downsampled segmentation mask. """ current = input_batch v_idx = 0 # Index variable. # Last block is the classification layer. for b_idx in xrange(len(dilations) - 1): for l_idx, dilation in enumerate(dilations[b_idx]): w = self.variables[v_idx * 2] b = self.variables[v_idx * 2 + 1] if dilation == 1: conv = tf.nn.conv2d(current, w, strides=[ 1, 1, 1, 1], padding='SAME') else: conv = tf.nn.atrous_conv2d( current, w, dilation, padding='SAME') current = tf.nn.relu(tf.nn.bias_add(conv, b)) v_idx += 1 # Optional pooling and dropout after each block. if b_idx < 3: current = tf.nn.max_pool(current, ksize=[1, ks, ks, 1], strides=[1, 2, 2, 1], padding='SAME') elif b_idx == 3: current = tf.nn.max_pool(current, ksize=[1, ks, ks, 1], strides=[1, 1, 1, 1], padding='SAME') elif b_idx == 4: current = tf.nn.max_pool(current, ksize=[1, ks, ks, 1], strides=[1, 1, 1, 1], padding='SAME') current = tf.nn.avg_pool(current, ksize=[1, ks, ks, 1], strides=[1, 1, 1, 1], padding='SAME') elif b_idx <= 6: current = tf.nn.dropout(current, keep_prob=keep_prob) # Classification layer; no ReLU. # w = self.variables[v_idx * 2] w = create_variable(name='w', shape=[1, 1, 1024, n_classes]) # b = self.variables[v_idx * 2 + 1] b = create_bias_variable(name='b', shape=[n_classes]) conv = tf.nn.conv2d(current, w, strides=[1, 1, 1, 1], padding='SAME') current = tf.nn.bias_add(conv, b) return current def prepare_label(self, input_batch, new_size): """Resize masks and perform one-hot encoding. Args: input_batch: input tensor of shape [batch_size H W 1]. new_size: a tensor with new height and width. Returns: Outputs a tensor of shape [batch_size h w 18] with last dimension comprised of 0's and 1's only. """ with tf.name_scope('label_encode'): # As labels are integer numbers, need to use NN interp. input_batch = tf.image.resize_nearest_neighbor( input_batch, new_size) # Reducing the channel dimension. input_batch = tf.squeeze(input_batch, squeeze_dims=[3]) input_batch = tf.one_hot(input_batch, depth=n_classes) return input_batch def preds(self, input_batch): """Create the network and run inference on the input batch. Args: input_batch: batch of pre-processed images. Returns: Argmax over the predictions of the network of the same shape as the input. """ raw_output = self._create_network( tf.cast(input_batch, tf.float32), keep_prob=tf.constant(1.0)) raw_output = tf.image.resize_bilinear( raw_output, tf.shape(input_batch)[1:3, ]) raw_output = tf.argmax(raw_output, dimension=3) raw_output = tf.expand_dims(raw_output, dim=3) # Create 4D-tensor. return tf.cast(raw_output, tf.uint8) def loss(self, img_batch, label_batch): """Create the network, run inference on the input batch and compute loss. Args: input_batch: batch of pre-processed images. Returns: Pixel-wise softmax loss. """ raw_output = self._create_network( tf.cast(img_batch, tf.float32), keep_prob=tf.constant(0.5)) prediction = tf.reshape(raw_output, [-1, n_classes]) # Need to resize labels and convert using one-hot encoding. label_batch = self.prepare_label( label_batch, tf.stack(raw_output.get_shape()[1:3])) gt = tf.reshape(label_batch, [-1, n_classes]) # Pixel-wise softmax loss. loss = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=gt) reduced_loss = tf.reduce_mean(loss) return reduced_loss ``` 按理说载入模型应该没有问题,可是不知道为什么结果却不一样? 图片:![图片说明](https://img-ask.csdn.net/upload/201911/15/1573810836_83106.jpg) ![图片说明](https://img-ask.csdn.net/upload/201911/15/1573810850_924663.png) 预测的结果: ![图片说明](https://img-ask.csdn.net/upload/201911/15/1573810884_985680.png) ![图片说明](https://img-ask.csdn.net/upload/201911/15/1573810904_577649.png) 两次结果不一样,与保存的模型算出来的结果也不一样。 我用的是GitHub上这个人的代码: https://github.com/minar09/DeepLab-LFOV-TensorFlow 急急急,请问有大神知道吗???
Mask RCNN训练过程中loss为nan的情况(使用labelme标注的数据)
1. 不是batchsize的问题,不是学习率的问题。我已经将学习率调成了0,结果也是这样,即迭代几次之后(不是一上来就是nan),loss就为nan了,但是后面5个loss正常收敛。 2. 训练类别数与数据集中的类别数一致。 * 想问问 帖子里面有大神知道原因,希望告知!多谢!! ![图片说明](https://img-ask.csdn.net/upload/201904/01/1554111703_91454.png)
为什么用vgg16网络训练我自己的数据集,loss一直在1.7左右震荡,用训练好的模型进行预测出来的都是一个值?
最近用vgg16网络训练自己的数据集,最开始的学习率设为了1e-3,发现loss到了1.7左右就不再降了,然后我调小学习率,调到了1e-5,loss到了1.7还是不降,一直在1.7左右震荡,训练了很多个epoch还是不变,于是我想测试看看,发现测试出来的图片分类结果都是一个值,请问有大佬知道这是为什么吗?
Picnic Cows 的实现的计算
Problem Description It’s summer vocation now. After tedious milking, cows are tired and wish to take a holiday. So Farmer Carolina considers having a picnic beside the river. But there is a problem, not all the cows consider it’s a good idea! Some cows like to swim in West Lake, some prefer to have a dinner in Shangri-la ,and others want to do something different. But in order to manage expediently, Carolina coerces all cows to have a picnic! Farmer Carolina takes her N (1<N≤400000) cows to the destination, but she finds every cow’s degree of interest in this activity is so different that they all loss their interests. So she has to group them to different teams to make sure that every cow can go to a satisfied team. Considering about the security, she demands that there must be no less than T(1<T≤N)cows in every team. As every cow has its own interest degree of this picnic, we measure this interest degree’s unit as “Moo~”. Cows in the same team should reduce their Moo~ to the one who has the lowest Moo~ in this team——It’s not a democratical action! So Carolina wishes to minimize the TOTAL reduced Moo~s and groups N cows into several teams. For example, Carolina has 7 cows to picnic and their Moo~ are ‘8 5 6 2 1 7 6’ and at least 3 cows in every team. So the best solution is that cow No.2,4,5 in a team (reduce (2-1)+(5-1) Moo~)and cow No.1,3,6,7 in a team (reduce ((7-6)+(8-6)) Moo~),the answer is 8. Input The input contains multiple cases. For each test case, the first line has two integer N, T indicates the number of cows and amount of Safe-base line. Following n numbers, describe the Moo~ of N cows , 1st is cow 1 , 2nd is cow 2, and so on. Output One line for each test case, containing one integer means the minimum of the TOTAL reduced Moo~s to group N cows to several teams. Sample Input 7 3 8 5 6 2 1 7 6 Sample Output 8
在Spyder界面中使用tensorflow进行fashion_mnist数据集学习,结果loss为非数,并且准确率一直未变
1.建立了一个3个全连接层的神经网络; 2.代码如下: ``` import matplotlib as mpl import matplotlib.pyplot as plt #%matplotlib inline import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow import keras print(tf.__version__) print(sys.version_info) for module in mpl, np, sklearn, tf, keras: print(module.__name__,module.__version__) fashion_mnist = keras.datasets.fashion_mnist (x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data() x_valid, x_train = x_train_all[:5000], x_train_all[5000:] y_valid, y_train = y_train_all[:5000], y_train_all[5000:] #tf.keras.models.Sequential model = keras.models.Sequential() model.add(keras.layers.Flatten(input_shape= [28,28])) model.add(keras.layers.Dense(300, activation="relu")) model.add(keras.layers.Dense(100, activation="relu")) model.add(keras.layers.Dense(10,activation="softmax")) ###sparse为最后输出为index类型,如果为one hot类型,则不需加sparse model.compile(loss = "sparse_categorical_crossentropy",optimizer = "sgd", metrics = ["accuracy"]) #model.layers #model.summary() history = model.fit(x_train, y_train, epochs=10, validation_data=(x_valid,y_valid)) ``` 3.输出结果: ``` runfile('F:/new/new world/deep learning/tensorflow/ex2/tf_keras_classification_model.py', wdir='F:/new/new world/deep learning/tensorflow/ex2') 2.0.0 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0) matplotlib 3.1.1 numpy 1.16.5 sklearn 0.21.3 tensorflow 2.0.0 tensorflow_core.keras 2.2.4-tf Train on 55000 samples, validate on 5000 samples Epoch 1/10 WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000025EAB633798> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: WARNING: Entity <function Function._initialize_uninitialized_variables.<locals>.initialize_variables at 0x0000025EAB633798> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 55000/55000 [==============================] - 3s 58us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 2/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 3/10 55000/55000 [==============================] - 3s 47us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 4/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 5/10 55000/55000 [==============================] - 3s 47us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 6/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 7/10 55000/55000 [==============================] - 3s 47us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 8/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 9/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 Epoch 10/10 55000/55000 [==============================] - 3s 48us/sample - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914 ```
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 ``` 以上的模型代码,请求各位大神帮我看看,为什么出现这样的结果?
Tensorflow中训练cifar10出现name 'train_data' is not defined问题
用神经网络训练cifar10 ``` init=tf.global_variables_initializer() batch_size=20 train_steps=1000 with tf.Session() as sess: for i in range(train_steps): batch_data,batch_labels=train_data.next_batch(batch_size) loss_val,acc_val,_=sess.run( [loss,accuracy,train_op], feed_dict= {x: batch_data, y: batch_labels}) if i%500==0: print('[Train] Step:%d,loss:%4.5f,acc:%4.5f'\ %(i,loss_val,acc_val)) # ``` 出现name 'train_data' is not defined问题不知道怎么解决了
请问tensorflow的训练的loss一直在1.几和0.几之间跳来跳去是算没收敛还是收敛了?
UCI上面找的训练集的输入是连续的,而标签是离散的,我见别人的LOSS都是持续下降到0.00几的,我这个把学习率调到0.001,激活函数是sigmoid,但还是在1.几和0.几之间徘徊,这是正常现象吗?不是的话是哪里出问题了? 跪求大佬解答!!
mlp 如何加载 doc2vec( .d2c)模型数据进行训练
mlp模型如下: ``` def MySimpleMLP(feature=700, vec_size=50): auc_roc = LSTM.as_keras_metric(tf.compat.v1.metrics.auc) model = Sequential() model.add(Flatten()) model.add(Dense(32, activation='relu', input_shape=(52,))) model.add(Dropout(0.2)) model.add(Dense(32, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(1, activation='softmax')) # compile model model.compile(loss="binary_crossentropy", optimizer="adam", metrics=[auc_roc]) return model ``` 训练函数如下: ``` model.fit(trainData, trainLabel, validation_split=0.2, epochs=10, batch_size=64, verbose=2) ``` do2vec模型是基于 imdb_50.d2v。 跪求各位大佬。
tensorflow训练过程权重不更新,loss不下降,输出保持不变,只有bias在非常缓慢地变化?
模型里没有参数被初始化为0 ,学习率从10的-5次方试到了0.1,输入数据都已经被归一化为了0-1之间,模型是改过的vgg16,有四个输出,使用了vgg16的预训练模型来初始化参数,输出中间结果也没有nan或者inf值。是不是不能自定义损失函数呢?但输出中间梯度发现并不是0,非常奇怪。 **train.py的部分代码** ``` def train(): x = tf.placeholder(tf.float32, [None, 182, 182, 2], name = 'image_input') y_ = tf.placeholder(tf.float32, [None, 8], name='label_input') global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(learning_rate=0.0001,decay_rate=0.9, global_step=TRAINING_STEPS, decay_steps=50,staircase=True) # 读取图片数据,pos是标签为1的图,neg是标签为0的图 pos, neg = get_data.get_image(img_path) #输入标签固定,输入数据每个batch前4张放pos,后4张放neg label_batch = np.reshape(np.array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]),[1, 8]) vgg = vgg16.Vgg16() vgg.build(x) #loss函数的定义在后面 loss = vgg.side_loss( y_,vgg.output1, vgg.output2, vgg.output3, vgg.output4) train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step) init_op = tf.global_variables_initializer() saver = tf.train.Saver() with tf.device('/gpu:0'): with tf.Session() as sess: sess.run(init_op) for i in range(TRAINING_STEPS): #在train.py的其他部分定义了batch_size= 4 start = i * batch_size end = start + batch_size #制作输入数据,前4个是标签为1的图,后4个是标签为0的图 image_list = [] image_list.append(pos[start:end]) image_list.append(neg[start:end]) image_batch = np.reshape(np.array(image_list),[-1,182,182,2]) _,loss_val,step = sess.run([train_step,loss,global_step], feed_dict={x: image_batch,y_:label_batch}) if i % 50 == 0: print("the step is %d,loss is %f" % (step, loss_val)) if loss_val < min_loss: min_loss = loss_val saver.save(sess, 'ckpt/vgg.ckpt', global_step=2000) ``` **Loss 函数的定义** ``` **loss函数的定义(写在了Vgg16类里)** ``` class Vgg16: #a,b,c,d都是vgg模型里的输出,是多输出模型 def side_loss(self,yi,a,b,c,d): self.loss1 = self.f_indicator(yi, a) self.loss2 = self.f_indicator(yi, b) self.loss3 = self.f_indicator(yi, c) self.loss_fuse = self.f_indicator(yi, d) self.loss_side = self.loss1 + self.loss2 + self.loss3 + self.loss_fuse res_loss = tf.reduce_sum(self.loss_side) return res_loss #损失函数的定义,标签为0时为log(1-yj),标签为1时为log(yj) def f_indicator(self,yi,yj): b = tf.where(yj>=1,yj*50,tf.abs(tf.log(tf.abs(1 - yj)))) res=tf.where(tf.equal(yi , 0.0), b,tf.abs(tf.log(tf.clip_by_value(yj, 1e-8, float("inf"))))) return res ```
cuda一个global函数里调用多个核函数出问题。
caffe编写loss层时, 我一个global函数里有多个核函数,但是有时前两个核函数不执行,有时候又执行,不清楚问题出在哪里? ``` template <typename Dtype> void PixelClustingLossLayer<Dtype>::Forward_gpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const int num = bottom[0]->num(); const int data_channels = bottom[0]->channels(); const int mask_channels = bottom[1]->channels(); const int height = bottom[0]->height(); const int width = bottom[0]->width(); const int spatial_dim = height * width; const int nc = num * data_channels; const int data_count = bottom[0]->count(); const int mask_count = bottom[1]->count(); Dtype* pos_num_data = pos_num_.mutable_cpu_data(); Dtype* neg_num_data = neg_num_.mutable_cpu_data(); caffe_gpu_set(mask_count, Dtype(0.), pixel_mask_.mutable_gpu_data()); caffe_gpu_set(num, Dtype(0.), loss_mask_.mutable_gpu_data()); caffe_gpu_set(num*data_channels, Dtype(0.), pos_ave_.mutable_gpu_data()); caffe_gpu_set(num*data_channels, Dtype(0.), neg_ave_.mutable_gpu_data()); caffe_gpu_set(num, Dtype(0.), pos_loss_.mutable_gpu_data()); caffe_gpu_set(num, Dtype(0.), neg_loss_.mutable_gpu_data()); caffe_gpu_set(num, Dtype(0.), center_loss_.mutable_gpu_data()); for(int n=0; n<num; ++n) { caffe_gpu_asum(spatial_dim, bottom[1]->gpu_data() + n * spatial_dim, pos_num_.mutable_cpu_data() + n); neg_num_data[n] = spatial_dim - pos_num_data[n]; } //LOG(INFO)<<"There are "<<pos_num_.cpu_data()[0]<<" pos pixels and "<<neg_num_.cpu_data()[0]<<" neg pixels."; GetTotalValue<Dtype> <<<CAFFE_GET_BLOCKS(data_count), CAFFE_CUDA_NUM_THREADS>>>(data_count, bottom[0]->gpu_data(), bottom[1]->gpu_data(), pos_ave_.mutable_gpu_data(), neg_ave_.mutable_gpu_data(), data_channels, height, width); //LOG(INFO)<<"There are 111 neg pixels."; GetAveValue<Dtype> <<<CAFFE_GET_BLOCKS(nc), CAFFE_CUDA_NUM_THREADS>>>(nc, pos_num_.gpu_data(), neg_num_.gpu_data(), pos_ave_.mutable_gpu_data(), neg_ave_.mutable_gpu_data(), center_loss_.mutable_gpu_data(), data_channels); //LOG(INFO)<<"There are 222 neg pixels."; PowerEuclideanDistance<Dtype> <<<CAFFE_GET_BLOCKS(mask_count), CAFFE_CUDA_NUM_THREADS>>>(mask_count, bottom[0]->gpu_data(), bottom[1]->gpu_data(), pos_ave_.gpu_data(), neg_ave_.gpu_data(), euclidean_dis_.mutable_gpu_data(), mask_channels, data_channels, height, width); ComputePixelLoss<Dtype> <<<CAFFE_GET_BLOCKS(mask_count), CAFFE_CUDA_NUM_THREADS>>>(mask_count, bottom[1]->gpu_data(), euclidean_dis_.gpu_data(), pos_loss_.mutable_gpu_data(), neg_loss_.mutable_gpu_data(), pos_num_.gpu_data(), neg_num_.gpu_data(), pixel_mask_.mutable_gpu_data(), mask_channels, height, width, alpha_); ComputeClassLoss<Dtype> <<<CAFFE_GET_BLOCKS(num), CAFFE_CUDA_NUM_THREADS>>>(num, center_loss_.mutable_gpu_data(), loss_mask_.mutable_gpu_data(), beta_); caffe_gpu_add(num, neg_loss_.gpu_data(), pos_loss_.gpu_data(), loss_.mutable_gpu_data()); caffe_gpu_add(num, loss_.gpu_data(), center_loss_.gpu_data(), loss_.mutable_gpu_data()); Dtype loss; caffe_gpu_asum(num, loss_.gpu_data(), &loss); LOG(INFO)<<loss/Dtype(num); top[0]->mutable_cpu_data()[0] = loss / num; } ``` 主要是GetTotalValue()函数和GetAveValue()函数,偶尔执行,偶尔不执行,头都晕了。 有没有大神指点迷津。
训练模型,测试集loss一直在降低,但测试集的acc也在降低,没有提高,这是过拟合了么?
我在训练模型的过程中,训练集表现很好,loss很低,acc很高。但测试的时候测试集的loss一直在降低,但acc反而也降低了,没有提高,这是过拟合了么?
卷积神经网络训练loss变为nan
卷积神经网络训练,用的是mnist数据集,第一次训练前损失函数还是一个值,训练一次之后就变成nan了,使用的损失函数是ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)),cem = tf.reduce.mean(ce),应该不会出现真数为零或负的情况,而且训练前loss是存在的,只是训练后变为nan,求各位大牛答疑解惑,感激不尽。![图片说明](https://img-ask.csdn.net/upload/201902/18/1550420090_522553.png)![图片说明](https://img-ask.csdn.net/upload/201902/18/1550420096_230838.png)![图片说明](https://img-ask.csdn.net/upload/201902/18/1550420101_914053.png)
keras model 训练 train_loss,train_acc再变,但是val_loss,val_test却一直不变,是哪里有问题?
Epoch 1/15 3112/3112 [==============================] - 73s 237ms/step - loss: 8.1257 - acc: 0.4900 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 2/15 3112/3112 [==============================] - 71s 231ms/step - loss: 8.1730 - acc: 0.4929 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 3/15 3112/3112 [==============================] - 72s 232ms/step - loss: 8.1730 - acc: 0.4929 - val_loss: 8.1763 - val_acc: 0.4427 Epoch 4/15 3112/3112 [==============================] - 71s 229ms/step - loss: 7.0495 - acc: 0.5617 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 5/15 3112/3112 [==============================] - 71s 230ms/step - loss: 5.5504 - acc: 0.6549 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 6/15 3112/3112 [==============================] - 71s 230ms/step - loss: 4.9359 - acc: 0.6931 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 7/15 3112/3112 [==============================] - 71s 230ms/step - loss: 4.8969 - acc: 0.6957 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 8/15 3112/3112 [==============================] - 72s 234ms/step - loss: 4.9446 - acc: 0.6925 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 9/15 3112/3112 [==============================] - 71s 231ms/step - loss: 4.5114 - acc: 0.7201 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 10/15 3112/3112 [==============================] - 73s 237ms/step - loss: 4.7944 - acc: 0.7021 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 11/15 3112/3112 [==============================] - 74s 240ms/step - loss: 4.6789 - acc: 0.7095 - val_loss: 8.1763 - val_acc: 0.4927
使用GPU训练深度神经网络,模型不收敛是什么原因?
无论是用caffe还是tensorflow来训练模型,只要是用GPU来训练,loss都不会下降,但同样情况下,用CPU来训练却没问题是什么原因呢?
关于使用深度强化学习Actor-Critic算法玩gym库中CartPole游戏不收敛的问题,高分悬赏。
小弟最近在自学深度强化学习,看的莫烦大佬的视频。其中有一个用AC算法玩gym库中CartPole的游戏实例,自己写的代码不知为何不能够收敛。考虑到自己自己写的程序中将AC网络写到一个类里去了,尝试过在A网络训练时截断C网络的梯度反向传播防止干扰,但还是不收敛。 小弟小白初学者自己瞎琢磨的,实在找不出原因,高分悬赏,希望大佬们能解惑。代码如下,其中有两个文件,一个是用以运行的主程序,另一个是主程序要调用的类,大佬们跑一下试试。 另外,真心诚意提问,请勿复制粘贴答非所问。 ``` ########主程序:AC_RL_run_this########## import gym from AC_RL_brain import ACNetwork def run_game(): step = 0 for episode in range(100000): episode_reward = 0 observation = env.reset() while True: if episode_reward > 20: env.render() action = RL.choose_action(observation) observation_, reward, done, _ = env.step(action) if done: reward = -20 RL.C_learn(observation, reward, observation_) RL.A_learn(observation, action) episode_reward += reward if done: break observation = observation_ step += 1 print('%d回合总回报:%f' % (episode, episode_reward)) print('game over') env.close() if __name__ == '__main__': env = gym.make('CartPole-v0') env.seed(1) RL = ACNetwork( n_actions=env.action_space.n, n_features=env.observation_space.shape[0], gamma=0.95, A_lr=0.001, C_lr=0.01, ) run_game() ########需要调用的类:AC_RL_brain########## import tensorflow as tf import numpy as np np.random.seed(2) tf.set_random_seed(2) # reproducible class ACNetwork: def __init__( self, n_actions, n_features, gamma, A_lr, C_lr, ): self.n_actions = n_actions self.n_features = n_features self.gamma = gamma self.A_lr = A_lr self.C_lr = C_lr self.td_error_real = 0 self._build_net() self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) def _build_net(self): # placeholder self.s = tf.placeholder(tf.float32, [1, self.n_features], "state") self.v_ = tf.placeholder(tf.float32, [1, 1], "v_next") self.r = tf.placeholder(tf.float32, None, 'r') self.a = tf.placeholder(tf.int32, None, "act") # A_net l1_A = tf.layers.dense( inputs=self.s, units=20, # number of hidden units activation=tf.nn.relu, kernel_initializer=tf.random_normal_initializer(0., .1), # weights bias_initializer=tf.constant_initializer(0.1), # biases ) self.acts_prob = tf.layers.dense( inputs=l1_A, units=self.n_actions, # output units activation=tf.nn.softmax, # get action probabilities kernel_initializer=tf.random_normal_initializer(0., .1), # weights bias_initializer=tf.constant_initializer(0.1), # biases ) self.log_prob = tf.log(self.acts_prob[0, self.a]) self.exp_v = tf.reduce_mean(self.log_prob * self.td_error_real) # advantage (TD_error) guided loss self.train_op_A = tf.train.AdamOptimizer(self.A_lr).minimize(-self.exp_v) # minimize(-exp_v) = maximize(exp_v) # C_net l1_C = tf.layers.dense( inputs=self.s, units=20, # number of hidden units activation=tf.nn.relu, # None # have to be linear to make sure the convergence of actor. # But linear approximator seems hardly learns the correct Q. kernel_initializer=tf.random_normal_initializer(0., .1), # weights bias_initializer=tf.constant_initializer(0.1), # biases ) self.v = tf.layers.dense( inputs=l1_C, units=1, # output units activation=None, kernel_initializer=tf.random_normal_initializer(0., .1), # weights bias_initializer=tf.constant_initializer(0.1), # biases ) self.td_error = self.r + self.gamma * self.v_ - self.v self.loss = tf.square(self.td_error) # TD_error = (r+gamma*V_next) - V_eval self.train_op_C = tf.train.AdamOptimizer(self.C_lr).minimize(self.loss) def choose_action(self, s): s = s[np.newaxis, :] probs = self.sess.run(self.acts_prob, {self.s: s}) # get probabilities for all actions return np.random.choice(np.arange(probs.shape[1]), p=probs.ravel()) # return a int def A_learn(self, s, a): s = s[np.newaxis, :] feed_dict = {self.s: s, self.a: a} _, exp_v = self.sess.run([self.train_op_A, self.exp_v], feed_dict) def C_learn(self, s, r, s_): s, s_ = s[np.newaxis, :], s_[np.newaxis, :] v_ = self.sess.run(self.v, {self.s: s_}) self.td_error_real, _ = self.sess.run([self.td_error, self.train_op_C], {self.s: s, self.v_: v_, self.r: r}) ```
相见恨晚的超实用网站
搞学习 知乎:www.zhihu.com 简答题:http://www.jiandati.com/ 网易公开课:https://open.163.com/ted/ 网易云课堂:https://study.163.com/ 中国大学MOOC:www.icourse163.org 网易云课堂:study.163.com 哔哩哔哩弹幕网:www.bilibili.com 我要自学网:www.51zxw
花了20分钟,给女朋友们写了一个web版群聊程序
参考博客 [1]https://www.byteslounge.com/tutorials/java-ee-html5-websocket-example
爬虫福利二 之 妹子图网MM批量下载
爬虫福利一:27报网MM批量下载    点击 看了本文,相信大家对爬虫一定会产生强烈的兴趣,激励自己去学习爬虫,在这里提前祝:大家学有所成! 目标网站:妹子图网 环境:Python3.x 相关第三方模块:requests、beautifulsoup4 Re:各位在测试时只需要将代码里的变量 path 指定为你当前系统要保存的路径,使用 python xxx.py 或IDE运行即可。
字节跳动视频编解码面经
引言 本文主要是记录一下面试字节跳动的经历。 三四月份投了字节跳动的实习(图形图像岗位),然后hr打电话过来问了一下会不会opengl,c++,shador,当时只会一点c++,其他两个都不会,也就直接被拒了。 七月初内推了字节跳动的提前批,因为内推没有具体的岗位,hr又打电话问要不要考虑一下图形图像岗,我说实习投过这个岗位不合适,不会opengl和shador,然后hr就说秋招更看重基础。我当时
开源一个功能完整的SpringBoot项目框架
福利来了,给大家带来一个福利。 最近想了解一下有关Spring Boot的开源项目,看了很多开源的框架,大多是一些demo或者是一个未成形的项目,基本功能都不完整,尤其是用户权限和菜单方面几乎没有完整的。 想到我之前做的框架,里面通用模块有:用户模块,权限模块,菜单模块,功能模块也齐全了,每一个功能都是完整的。 打算把这个框架分享出来,供大家使用和学习。 为什么用框架? 框架可以学习整体
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过
Python——画一棵漂亮的樱花树(不同种樱花+玫瑰+圣诞树喔)
最近翻到一篇知乎,上面有不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成喔~) one 樱花树 动态生成樱花 效果图(这个是动态的): 实现代码 import turtle as T import random import time # 画樱花的躯干(60,t) def Tree(branch
深深的码丨Java HashMap 透析
HashMap 相关概念 HashTab、HashMap、TreeMap 均以键值对像是存储或操作数据元素。HashTab继承自Dictionary,HashMap、TreeMap继承自AbstractMap,三者均实现Map接口 **HashTab:**同步哈希表,不支持null键或值,因为同步导致性能影响,很少被使用 **HashMap:**应用较多的非同步哈希表,支持null键或值,是键值对...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 顺便拉下票,我在参加csdn博客之星竞选,欢迎投票支持,每个QQ或者微信每天都可以投5票,扫二维码即可,http://m234140.nofollow.ax.
Python 基础(一):入门必备知识
目录1 标识符2 关键字3 引号4 编码5 输入输出6 缩进7 多行8 注释9 数据类型10 运算符10.1 常用运算符10.2 运算符优先级 1 标识符 标识符是编程时使用的名字,用于给变量、函数、语句块等命名,Python 中标识符由字母、数字、下划线组成,不能以数字开头,区分大小写。 以下划线开头的标识符有特殊含义,单下划线开头的标识符,如:_xxx ,表示不能直接访问的类属性,需通过类提供
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 欢迎 改进 留言。 演示地点跳到演示地点 html代码如下`&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;music&lt;/title&gt; &lt;meta charset="utf-8"&gt
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。 1. for - else 什么?不是 if 和 else 才
数据库优化 - SQL优化
前面一篇文章从实例的角度进行数据库优化,通过配置一些参数让数据库性能达到最优。但是一些“不好”的SQL也会导致数据库查询变慢,影响业务流程。本文从SQL角度进行数据库优化,提升SQL运行效率。 判断问题SQL 判断SQL是否有问题时可以通过两个表象进行判断: 系统级别表象 CPU消耗严重 IO等待严重 页面响应时间过长
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 c/c++ 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7
通俗易懂地给女朋友讲:线程池的内部原理
餐厅的约会 餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”我楞了一下,心里想女朋友今天是怎么了,怎么突然问出这么专业的问题,但做为一个专业人士在女朋友面前也不能露怯啊,想了一下便说:“我先给你讲讲我前同事老王的故事吧!” 大龄程序员老王 老王是一个已经北漂十多年的程序员,岁数大了,加班加不动了,升迁也无望,于是拿着手里
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹
面试官:你连RESTful都不知道我怎么敢要你?
面试官:了解RESTful吗? 我:听说过。 面试官:那什么是RESTful? 我:就是用起来很规范,挺好的 面试官:是RESTful挺好的,还是自我感觉挺好的 我:都挺好的。 面试官:… 把门关上。 我:… 要干嘛?先关上再说。 面试官:我说出去把门关上。 我:what ?,夺门而去 文章目录01 前言02 RESTful的来源03 RESTful6大原则1. C-S架构2. 无状态3.统一的接
JDK12 Collectors.teeing 你真的需要了解一下
前言 在 Java 12 里面有个非常好用但在官方 JEP 没有公布的功能,因为它只是 Collector 中的一个小改动,它的作用是 merge 两个 collector 的结果,这句话显得很抽象,老规矩,我们先来看个图(这真是一个不和谐的图????): 管道改造经常会用这个小东西,通常我们叫它「三通」,它的主要作用就是将 downstream1 和 downstre...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
致 Python 初学者
欢迎来到“Python进阶”专栏!来到这里的每一位同学,应该大致上学习了很多 Python 的基础知识,正在努力成长的过程中。在此期间,一定遇到了很多的困惑,对未来的学习方向感到迷茫。我非常理解你们所面临的处境。我从2007年开始接触 python 这门编程语言,从2009年开始单一使用 python 应对所有的开发工作,直至今天。回顾自己的学习过程,也曾经遇到过无数的困难,也曾经迷茫过、困惑过。开办这个专栏,正是为了帮助像我当年一样困惑的 Python 初学者走出困境、快速成长。希望我的经验能真正帮到你
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1...
程序员把地府后台管理系统做出来了,还有3.0版本!12月7号最新消息:已在开发中有github地址
第一幕:缘起 听说阎王爷要做个生死簿后台管理系统,我们派去了一个程序员…… 996程序员做的梦: 第一场:团队招募 为了应对地府管理危机,阎王打算找“人”开发一套地府后台管理系统,于是就在地府总经办群中发了项目需求。 话说还是中国电信的信号好,地府都是满格,哈哈!!! 经常会有外行朋友问:看某网站做的不错,功能也简单,你帮忙做一下? 而这次,面对这样的需求,这个程序员...
相关热词 c# 图片上传 c# gdi 占用内存 c#中遍历字典 c#控制台模拟dos c# 斜率 最小二乘法 c#进程延迟 c# mysql完整项目 c# grid 总行数 c# web浏览器插件 c# xml 生成xsd
立即提问

相似问题

1
卷积神经网络训练loss变为nan
1
请问tensorflow的训练的loss一直在1.几和0.几之间跳来跳去是算没收敛还是收敛了?
2
度量学习中三元组损失不收敛(loss无法下降到margin以下,样本的降维输出聚在一起)
1
Mask RCNN训练过程中loss为nan的情况(使用labelme标注的数据)
1
修改的SSD—Tensorflow 版本在训练的时候遇到loss输入维度不一致
1
tensorflow训练过程权重不更新,loss不下降,输出保持不变,只有bias在非常缓慢地变化?
1
tensorflow自定义的损失函数 focal_loss出现inf,在训练过程中出现inf
1
训练模型,测试集loss一直在降低,但测试集的acc也在降低,没有提高,这是过拟合了么?
1
keras model 训练 train_loss,train_acc再变,但是val_loss,val_test却一直不变,是哪里有问题?
1
神经网络按梯度下降训练,不应该用同一个样本迭代直到得到loss最小值?然后再继续下一个样本吗?
1
关于使用深度强化学习Actor-Critic算法玩gym库中CartPole游戏不收敛的问题,高分悬赏。
1
训练网络时损失值一直震荡
1
为什么用vgg16网络训练我自己的数据集,loss一直在1.7左右震荡,用训练好的模型进行预测出来的都是一个值?
1
tensorflow 里loss 出现nan问题 新手问题
0
tensorflow载入训练好的模型进行预测,同一张图片预测的结果却不一样????
1
Tensorflow中训练cifar10出现name 'train_data' is not defined问题
1
训练准确率很高,测试准确率很低(loss有一直下降)
0
在Spyder界面中使用tensorflow进行fashion_mnist数据集学习,结果loss为非数,并且准确率一直未变
1
tensorflow训练完模型直接测试和导入模型进行测试的结果不同,一个很好,一个略差,这是为什么?
1
mlp 如何加载 doc2vec( .d2c)模型数据进行训练