大家能帮忙看一下这个tf.nn.sparse_softmax_cross_entropy_with_logits的输入都是几维的么?

大家能帮忙看一下这个tf.nn.sparse_softmax_cross_entropy_with_logits的输入都是几维的么?为什么看注释的话好像预测值和标签值维度是不一样的阿图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
卷积神经网络训练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)

CNN算法中怎么使用自调节学习速率

``` ........ #---------------------------网络结束--------------------------- loss=tf.losses.sparse_softmax_cross_entropy(labels=y_,logits=logits) train_op=tf.train.AdamOptimizer(learning_rate=0.05).minimize(loss) correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_) acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) ...... for epoch in range(n_epoch): ...... #training train_loss, train_acc, n_batch = 0, 0, 0 for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True): _,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a}) train_loss += err; train_acc += ac; n_batch += 1 print(" train loss: %f" % (train_loss/ n_batch)) print(" train acc: %f" % (train_acc/ n_batch)) ...... ``` 怎么能在每次循环里用 初始学习速率/循环次数 (0.05/epoch) 作为当前学习速率,以让下降速率递减

修改的SSD—Tensorflow 版本在训练的时候遇到loss输入维度不一致

目前在学习目标检测识别的方向。 自己参考了一些论文 对原版的SSD进行了一些改动工作 前面的网络模型部分已经修改完成且不报错。 但是在进行训练操作的时候会出现 ’ValueError: Dimension 0 in both shapes must be equal, but are 233920 and 251392. Shapes are [233920] and [251392]. for 'ssd_losses/Select' (op: 'Select') with input shapes: [251392], [233920], [251392]. ‘ ‘两个形状中的尺寸0必须相等,但分别为233920和251392。形状有[233920]和[251392]。对于输入形状为[251392]、[233920]、[251392]的''ssd_losses/Select' (op: 'Select') ![图片说明](https://img-ask.csdn.net/upload/201904/06/1554539638_631515.png) ![图片说明](https://img-ask.csdn.net/upload/201904/06/1554539651_430990.png) # SSD loss function. # =========================================================================== # def ssd_losses(logits, localisations, gclasses, glocalisations, gscores, match_threshold=0.5, negative_ratio=3., alpha=1., label_smoothing=0., device='/cpu:0', scope=None): with tf.name_scope(scope, 'ssd_losses'): lshape = tfe.get_shape(logits[0], 5) num_classes = lshape[-1] batch_size = lshape[0] # Flatten out all vectors! flogits = [] fgclasses = [] fgscores = [] flocalisations = [] fglocalisations = [] for i in range(len(logits)): flogits.append(tf.reshape(logits[i], [-1, num_classes])) fgclasses.append(tf.reshape(gclasses[i], [-1])) fgscores.append(tf.reshape(gscores[i], [-1])) flocalisations.append(tf.reshape(localisations[i], [-1, 4])) fglocalisations.append(tf.reshape(glocalisations[i], [-1, 4])) # And concat the crap! logits = tf.concat(flogits, axis=0) gclasses = tf.concat(fgclasses, axis=0) gscores = tf.concat(fgscores, axis=0) localisations = tf.concat(flocalisations, axis=0) glocalisations = tf.concat(fglocalisations, axis=0) dtype = logits.dtype # Compute positive matching mask... pmask = gscores > match_threshold fpmask = tf.cast(pmask, dtype) n_positives = tf.reduce_sum(fpmask) # Hard negative mining... no_classes = tf.cast(pmask, tf.int32) predictions = slim.softmax(logits) nmask = tf.logical_and(tf.logical_not(pmask), gscores > -0.5) fnmask = tf.cast(nmask, dtype) nvalues = tf.where(nmask, predictions[:, 0], 1. - fnmask) nvalues_flat = tf.reshape(nvalues, [-1]) # Number of negative entries to select. max_neg_entries = tf.cast(tf.reduce_sum(fnmask), tf.int32) n_neg = tf.cast(negative_ratio * n_positives, tf.int32) + batch_size n_neg = tf.minimum(n_neg, max_neg_entries) val, idxes = tf.nn.top_k(-nvalues_flat, k=n_neg) max_hard_pred = -val[-1] # Final negative mask. nmask = tf.logical_and(nmask, nvalues < max_hard_pred) fnmask = tf.cast(nmask, dtype) # Add cross-entropy loss. with tf.name_scope('cross_entropy_pos'): loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=gclasses) loss = tf.div(tf.reduce_sum(loss * fpmask), batch_size, name='value') tf.losses.add_loss(loss) with tf.name_scope('cross_entropy_neg'): loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=no_classes) loss = tf.div(tf.reduce_sum(loss * fnmask), batch_size, name='value') tf.losses.add_loss(loss) # Add localization loss: smooth L1, L2, ... with tf.name_scope('localization'): # Weights Tensor: positive mask + random negative. weights = tf.expand_dims(alpha * fpmask, axis=-1) loss = custom_layers.abs_smooth(localisations - glocalisations) loss = tf.div(tf.reduce_sum(loss * weights), batch_size, name='value') tf.losses.add_loss(loss) ``` ``` 研究了一段时间的源码 (因为只是SSD-Tensorflow-Master中的ssd_vgg_300.py中定义网络结构的那部分做了修改 ,loss函数代码部分并没有进行改动)所以没所到错误所在,网上也找不到相关的解决方案。 希望大神能够帮忙解答 感激不尽~

optim.compute_gradients计算梯度 ,为什么返回的第一列为None?

1.问题描述 模型通过keras.models.Sequential构建 loss => tf.losses.sparse_softmax_cross_entropy 通过 var_list=tf.trainable_variables() 获取可训练变量 计算梯度值: loss_op = tf.losses.sparse_softmax_cross_entropy(y, y_pred) grads_vars = optim.compute_gradients(loss_op, tf.trainable_variables()) grads_vars返回的第一元素为 None,导致面的程序异常。 为什么grads_vars第一列返回的元素为None? 2.相关代码 ``` import tensorflow as tf import numpy as np import time import keras # 加载数据集 x_dataset=np.random.rand(1000,28,28,1) y_dataset=np.random.randint(0,10,size=(1000,)) act = tf.nn.leaky_relu epoch = 200 batch_size = 5000 n_batch = len(x_dataset) // batch_size # 把 batch 分成多少个 sub batch 来计算 subdivisions = 50 subdivisions_batch_size = int(np.ceil(batch_size / subdivisions)) # 是否使用 sub batch 方法,设置为 False 代表使用默认方法 is_on_subdivisions = True def get_model(is_train=True, reuse=False): with tf.variable_scope('model', reuse=reuse): net = keras.models.Sequential() net.add(keras.layers.Conv2D(128,(3,3),input_shape=(28,28,1),strides=(2,2),padding='same',name='c1')) net.add(keras.layers.GlobalAveragePooling2D()) net.add(keras.layers.Dense(10)) return net x = tf.placeholder(tf.float32, [None, 28, 28, 1]) y = tf.placeholder(tf.int32, [None,]) net = get_model() y_pred=tf.cast(tf.argmax(net.outputs[0],axis=-1),dtype=tf.float32) loss_op = tf.losses.sparse_softmax_cross_entropy(y, y_pred) optim = tf.train.AdamOptimizer(0.01) var_list=tf.trainable_variables() grads_vars = optim.compute_gradients(loss_op, tf.trainable_variables()) #grads_vars返回的第一列为None,为什么? for gv in grads_vars: print(gv) # 删掉没梯度的参数, 倒序删除,减少麻烦 for i in range(len(grads_vars))[::-1]: if grads_vars[i][0] is None: del grads_vars[i] #因为返回的第一列为None,所以所有变量都被删除了,导致后面的异常! print('len(grads_vars):',len(grads_vars)) # 生成梯度缓存:grads_vars第一列为None触发异常 grads_cache = [tf.Variable(np.zeros(t[0].shape.as_list(), np.float32), trainable=False) for t in grads_vars] # 清空梯度缓存op,每一 batch 开始前调用 clear_grads_cache_op = tf.group([gc.assign(tf.zeros_like(gc)) for gc in grads_cache]) # 累积梯度op,累积每个 sub batch 的梯度 accumulate_grad_op = tf.group([gc.assign_add(gv[0]) for gc, gv in zip(grads_cache, grads_vars)]) # 求平均梯度, mean_grad = [gc/tf.to_float(subdivisions) for gc in grads_cache] # 组装梯度列表 new_grads_vars = [(g, gv[1]) for g, gv in zip(mean_grad, grads_vars)] # 应用梯度op,累积完所有 sub batch 的梯度后,应用梯度 apply_grad_op = optim.apply_gradients(new_grads_vars) # 原来的 optim ,跟上面做对照 ori_optim_op = tf.train.AdamOptimizer(0.01).minimize(loss_op, var_list=net.all_params) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) for e in range(epoch): loss_sum = 0 for b in progressbar(range(n_batch)): x_batch = x_dataset[b * batch_size: (b + 1) * batch_size] y_batch = y_dataset[b * batch_size: (b + 1) * batch_size] if is_on_subdivisions: # 每一批开始前需要清空梯度缓存 sess.run(clear_grads_cache_op) sub_loss_sum = 0 for s in range(subdivisions): x_sub_batch = x_batch[s * subdivisions_batch_size: (s + 1) * subdivisions_batch_size] y_sub_batch = y_batch[s * subdivisions_batch_size: (s + 1) * subdivisions_batch_size] if len(x_sub_batch) == 0: break feed_dict = {x: x_sub_batch, y: y_sub_batch} _, los = sess.run([accumulate_grad_op, loss_op], feed_dict) sub_loss_sum += los loss_sum += sub_loss_sum / subdivisions # 梯度累积完成,开始应用梯度 sess.run(apply_grad_op) # 本批次结束 else: feed_dict = {x: x_batch, y: y_batch} _, los = sess.run([ori_optim_op, loss_op], feed_dict) loss_sum += los time.sleep(0.2) print('loss', loss_sum / n_batch) ``` 3.报错信息 ``` grads_vars: (None, <tf.Variable 'model/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model/dense_1/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model/dense_1/bias:0' shape=(10,) dtype=float32_ref>) (None, <tf.Variable 'model_1/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model_1/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model_1/dense_2/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model_1/dense_2/bias:0' shape=(10,) dtype=float32_ref>) (None, <tf.Variable 'model_2/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model_2/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model_2/dense_3/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model_2/dense_3/bias:0' shape=(10,) dtype=float32_ref>) len(grads_vars): 0 ``` 4.尝试过的方法方式 5.相关截图

多分类softmax问题使用binary_crossentropy

我做了个语音5分类的实验,最后是softmax函数进行5分类,最后使用了binary_crossentropy作为loss函数,结果意外发现结果会高好多,是不是这样跑出来的数据有问题啊?

在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.python.framework.errors_impl.InvalidArgumentError

tensorflow.python.framework.errors_impl.InvalidArgumentError: Key: image/encoded. Can't parse serialized Example. [[Node: ParseSingleExample/ParseSingleExample = ParseSingleExample[Tdense=[DT_STRING, DT_INT64], dense_keys=["image/encoded", "image/label"], dense_shapes=[[8], []], num_sparse=0, sparse_keys=[], sparse_types=[]](arg0, ParseSingleExample/Const, ParseSingleExample/Const_1)]] [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,8,?,?,3], [?]], output_types=[DT_FLOAT, DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]] 请问这是什么原因啊?是我的数据集不对吗?

Tensorflow 多GPU并行训练,模型收敛su'du'ma

在使用多GPU并行训练深度学习神经网络时,以TFrecords 形式读取MNIST数据的训练数据进行训练,发现比直接用MNIST训练数据训练相同模型时,发现前者收敛速度慢和运行时间长,已知模型没有问题,想请大神帮忙看看是什么原因导致运行速度慢,运行时间长 ``` import os import time import numpy as np import tensorflow as tf from datetime import datetime import tensorflow.compat.v1 as v1 from tensorflow.examples.tutorials.mnist import input_data BATCH_SIZE = 100 LEARNING_RATE = 1e-4 LEARNING_RATE_DECAY = 0.99 REGULARZTION_RATE = 1e-4 EPOCHS = 10000 MOVING_AVERAGE_DECAY = 0.99 N_GPU = 2 MODEL_SAVE_PATH = r'F:\model\log_dir' MODEL_NAME = 'model.ckpt' TRAIN_PATH = r'F:\model\threads_file\MNIST_data_tfrecords\train.tfrecords' TEST_PATH = r'F:\model\threads_file\MNIST_data_tfrecords\test.tfrecords' def __int64_feature(value): return v1.train.Feature(int64_list=v1.train.Int64List(value=[value])) def __bytes_feature(value): return v1.train.Feature(bytes_list=v1.train.BytesList(value=[value])) def creat_tfrecords(path, data, labels): writer = tf.io.TFRecordWriter(path) for i in range(len(data)): image = data[i].tostring() label = labels[i] examples = v1.train.Example(features=v1.train.Features(feature={ 'image': __bytes_feature(image), 'label': __int64_feature(label) })) writer.write(examples.SerializeToString()) writer.close() def parser(record): features = v1.parse_single_example(record, features={ 'image': v1.FixedLenFeature([], tf.string), 'label': v1.FixedLenFeature([], tf.int64) }) image = tf.decode_raw(features['image'], tf.uint8) image = tf.reshape(image, [28, 28, 1]) image = tf.cast(image, tf.float32) label = tf.cast(features['label'], tf.int32) label = tf.one_hot(label, 10, on_value=1, off_value=0) return image, label def get_input(batch_size, path): dataset = tf.data.TFRecordDataset([path]) dataset = dataset.map(parser) dataset = dataset.shuffle(10000) dataset = dataset.repeat(100) dataset = dataset.batch(batch_size) iterator = dataset.make_one_shot_iterator() image, label = iterator.get_next() return image, label def model_inference(images, labels, rate, regularzer=None, reuse_variables=None): with v1.variable_scope(v1.get_variable_scope(), reuse=reuse_variables): with tf.compat.v1.variable_scope('First_conv'): w1 = tf.compat.v1.get_variable('weights', [3, 3, 1, 32], tf.float32, initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1)) if regularzer: tf.add_to_collection('losses', regularzer(w1)) b1 = tf.compat.v1.get_variable('biases', [32], tf.float32, initializer=tf.compat.v1.constant_initializer(0.1)) activation1 = tf.nn.relu(tf.nn.conv2d(images, w1, strides=[1, 1, 1, 1], padding='SAME') + b1) out1 = tf.nn.max_pool2d(activation1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') with tf.compat.v1.variable_scope('Second_conv'): w2 = tf.compat.v1.get_variable('weight', [3, 3, 32, 64], tf.float32, initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1)) if regularzer: tf.add_to_collection('losses', regularzer(w2)) b2 = tf.compat.v1.get_variable('biases', [64], tf.float32, initializer=tf.compat.v1.constant_initializer(0.1)) activation2 = tf.nn.relu(tf.nn.conv2d(out1, w2, strides=[1, 1, 1, 1], padding='SAME') + b2) out2 = tf.nn.max_pool2d(activation2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') out3 = tf.reshape(out2, [-1, 7*7*64], name='flatten') with tf.compat.v1.variable_scope('FC_1'): w3 = tf.compat.v1.get_variable('weight', [7*7*64, 1024], tf.float32, initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1)) if regularzer: tf.add_to_collection('losses', regularzer(w3)) b3 = tf.compat.v1.get_variable('biases', [1024], tf.float32, initializer=tf.compat.v1.constant_initializer(0.1)) activation3 = tf.nn.relu(tf.matmul(out3, w3) + b3) out4 = tf.nn.dropout(activation3, keep_prob=rate) with tf.compat.v1.variable_scope('FC_2'): w4 = tf.compat.v1.get_variable('weight', [1024, 10], tf.float32, initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1)) if regularzer: tf.add_to_collection('losses', regularzer(w4)) b4 = tf.compat.v1.get_variable('biases', [10], tf.float32, initializer=tf.compat.v1.constant_initializer(0.1)) output = tf.nn.softmax(tf.matmul(out4, w4) + b4) with tf.compat.v1.variable_scope('Loss_entropy'): if regularzer: loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(labels, 1), logits=output)) \ + tf.add_n(tf.get_collection('losses')) else: loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(labels, 1), logits=output)) with tf.compat.v1.variable_scope('Accuracy'): correct_data = tf.equal(tf.math.argmax(labels, 1), tf.math.argmax(output, 1)) accuracy = tf.reduce_mean(tf.cast(correct_data, tf.float32, name='accuracy')) return output, loss, accuracy def average_gradients(tower_grads): average_grads = [] for grad_and_vars in zip(*tower_grads): grads = [] for g, v2 in grad_and_vars: expanded_g = tf.expand_dims(g, 0) grads.append(expanded_g) grad = tf.concat(grads, 0) grad = tf.reduce_mean(grad, 0) v = grad_and_vars[0][1] grad_and_var = (grad, v) average_grads.append(grad_and_var) return average_grads def main(argv=None): with tf.Graph().as_default(), tf.device('/cpu:0'): x, y = get_input(batch_size=BATCH_SIZE, path=TRAIN_PATH) regularizer = tf.contrib.layers.l2_regularizer(REGULARZTION_RATE) global_step = v1.get_variable('global_step', [], initializer=v1.constant_initializer(0), trainable=False) lr = v1.train.exponential_decay(LEARNING_RATE, global_step, 55000/BATCH_SIZE, LEARNING_RATE_DECAY) opt = v1.train.AdamOptimizer(lr) tower_grads = [] reuse_variables = False device = ['/gpu:0', '/cpu:0'] for i in range(len(device)): with tf.device(device[i]): with v1.name_scope(device[i][1:4] + '_0') as scope: out, cur_loss, acc = model_inference(x, y, 0.3, regularizer, reuse_variables) reuse_variables = True grads = opt.compute_gradients(cur_loss) tower_grads.append(grads) grads = average_gradients(tower_grads) for grad, var in grads: if grad is not None: v1.summary.histogram('gradients_on_average/%s' % var.op.name, grad) apply_gradient_op = opt.apply_gradients(grads, global_step) for var in v1.trainable_variables(): tf.summary.histogram(var.op.name, var) variable_averages = v1.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) variable_to_average = (v1.trainable_variables() + v1.moving_average_variables()) variable_averages_op = variable_averages.apply(variable_to_average) train_op = tf.group(apply_gradient_op, variable_averages_op) saver = v1.train.Saver(max_to_keep=1) summary_op = v1.summary.merge_all() # merge_all 可以将所有summary全部保存到磁盘 init = v1.global_variables_initializer() with v1.Session(config=v1.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess: init.run() summary_writer = v1.summary.FileWriter(MODEL_SAVE_PATH, sess.graph) # 指定一个文件用来保存图 for step in range(EPOCHS): try: start_time = time.time() _, loss_value, out_value, acc_value = sess.run([train_op, cur_loss, out, acc]) duration = time.time() - start_time if step != 0 and step % 100 == 0: num_examples_per_step = BATCH_SIZE * N_GPU examples_per_sec = num_examples_per_step / duration sec_per_batch = duration / N_GPU format_str = '%s: step %d, loss = %.2f(%.1f examples/sec; %.3f sec/batch), accuracy = %.2f' print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch, acc_value)) summary = sess.run(summary_op) summary_writer.add_summary(summary, step) if step % 100 == 0 or (step + 1) == EPOCHS: checkpoint_path = os.path.join(MODEL_SAVE_PATH, MODEL_NAME) saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: break if __name__ == '__main__': tf.app.run() ```

求助,Tensorflow搭建AlexNet模型是训练集验证集的LOSS不收敛

如题,代码如下,请大佬赐教 ``` # coding:utf-8 import tensorflow as tf import numpy as np import time import os import cv2 import matplotlib.pyplot as plt def get_file(file_dir): images = [] labels = [] for root, sub_folders, files in os.walk(file_dir): for name in files: images.append(os.path.join(root, name)) letter = name.split('.')[0] # 对标签进行分类 if letter == 'cat': labels = np.append(labels, [0]) else: labels = np.append(labels, [1]) # shuffle(随机打乱) temp = np.array([images, labels]) temp = temp.transpose() # 建立images 与 labels 之间关系, 以矩阵形式展现 np.random.shuffle(temp) image_list = list(temp[:, 0]) label_list = list(temp[:, 1]) label_list = [int(float(i)) for i in label_list] print(image_list) print(label_list) return image_list, label_list # 返回文件名列表 def _parse_function(image_list, labels_list): image_contents = tf.read_file(image_list) image = tf.image.decode_jpeg(image_contents, channels=3) image = tf.cast(image, tf.float32) image = tf.image.resize_image_with_crop_or_pad(image, 227, 227) # 剪裁或填充处理 image = tf.image.per_image_standardization(image) # 图片标准化 labels = labels_list return image, labels # 将需要读取的数据集地址转换为专用格式 def get_batch(image_list, labels_list, batch_size): image_list = tf.cast(image_list, tf.string) labels_list = tf.cast(labels_list, tf.int32) dataset = tf.data.Dataset.from_tensor_slices((image_list, labels_list)) # 创建dataset dataset = dataset.repeat() # 无限循环 dataset = dataset.map(_parse_function) dataset = dataset.batch(batch_size) dataset = dataset.make_one_shot_iterator() return dataset # 正则化处理数据集 def batch_norm(inputs, is_training, is_conv_out=True, decay=0.999): scale = tf.Variable(tf.ones([inputs.get_shape()[-1]])) beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]])) pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]), trainable=False) pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]), trainable=False) def batch_norm_train(): if is_conv_out: batch_mean, batch_var = tf.nn.moments(inputs, [0, 1, 2]) # 求均值及方差 else: batch_mean, batch_var = tf.nn.moments(inputs, [0]) train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean * (1 - decay)) train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1 - decay)) with tf.control_dependencies([train_mean, train_var]): # 在train_mean, train_var计算完条件下继续 return tf.nn.batch_normalization(inputs, batch_mean, batch_var, beta, scale, 0.001) def batch_norm_test(): return tf.nn.batch_normalization(inputs, pop_mean, pop_var, beta, scale, 0.001) batch_normalization = tf.cond(is_training, batch_norm_train, batch_norm_test) return batch_normalization # 建立模型 learning_rate = 1e-4 training_iters = 200 batch_size = 50 display_step = 5 n_classes = 2 n_fc1 = 4096 n_fc2 = 2048 # 构建模型 x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.int32, [None]) is_training = tf.placeholder(tf.bool) # 字典模式管理权重与偏置参数 W_conv = { 'conv1': tf.Variable(tf.truncated_normal([11, 11, 3, 96], stddev=0.0001)), 'conv2': tf.Variable(tf.truncated_normal([5, 5, 96, 256], stddev=0.01)), 'conv3': tf.Variable(tf.truncated_normal([3, 3, 256, 384], stddev=0.01)), 'conv4': tf.Variable(tf.truncated_normal([3, 3, 384, 384], stddev=0.01)), 'conv5': tf.Variable(tf.truncated_normal([3, 3, 384, 256], stddev=0.01)), 'fc1': tf.Variable(tf.truncated_normal([6 * 6 * 256, n_fc1], stddev=0.1)), 'fc2': tf.Variable(tf.truncated_normal([n_fc1, n_fc2], stddev=0.1)), 'fc3': tf.Variable(tf.truncated_normal([n_fc2, n_classes], stddev=0.1)), } b_conv = { 'conv1': tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[96])), 'conv2': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[256])), 'conv3': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[384])), 'conv4': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[384])), 'conv5': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[256])), 'fc1': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[n_fc1])), 'fc2': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[n_fc2])), 'fc3': tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[n_classes])), } x_image = tf.reshape(x, [-1, 227, 227, 3]) # 卷积层,池化层,LRN层编写 # 第一层卷积层 # 卷积层1 conv1 = tf.nn.conv2d(x_image, W_conv['conv1'], strides=[1, 4, 4, 1], padding='VALID') conv1 = tf.nn.bias_add(conv1, b_conv['conv1']) conv1 = batch_norm(conv1, is_training) #conv1 = tf.layers.batch_normalization(conv1, training=is_training) conv1 = tf.nn.relu(conv1) # 池化层1 pool1 = tf.nn.avg_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # LRN层 norm1 = tf.nn.lrn(pool1, 5, bias=1.0, alpha=0.001 / 9.0, beta=0.75) # 第二层卷积 # 卷积层2 conv2 = tf.nn.conv2d(norm1, W_conv['conv2'], strides=[1, 1, 1, 1], padding='SAME') conv2 = tf.nn.bias_add(conv2, b_conv['conv2']) #conv2 = tf.layers.batch_normalization(conv2, training=is_training) conv2 = batch_norm(conv2, is_training) conv2 = tf.nn.relu(conv2) # 池化层2 pool2 = tf.nn.avg_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # LRN层 #norm2 = tf.nn.lrn(pool2, 5, bias=1.0, alpha=0.001 / 9.0, beta=0.75) # 第三层卷积 # 卷积层3 conv3 = tf.nn.conv2d(pool2, W_conv['conv3'], strides=[1, 1, 1, 1], padding='SAME') conv3 = tf.nn.bias_add(conv3, b_conv['conv3']) #conv3 = tf.layers.batch_normalization(conv3, training=is_training) conv3 = batch_norm(conv3, is_training) conv3 = tf.nn.relu(conv3) # 第四层卷积 # 卷积层4 conv4 = tf.nn.conv2d(conv3, W_conv['conv4'], strides=[1, 1, 1, 1], padding='SAME') conv4 = tf.nn.bias_add(conv4, b_conv['conv4']) #conv4 = tf.layers.batch_normalization(conv4, training=is_training) conv4 = batch_norm(conv4, is_training) conv4 = tf.nn.relu(conv4) # 第五层卷积 # 卷积层5 conv5 = tf.nn.conv2d(conv4, W_conv['conv5'], strides=[1, 1, 1, 1], padding='SAME') conv5 = tf.nn.bias_add(conv5, b_conv['conv5']) #conv5 = tf.layers.batch_normalization(conv5, training=is_training) conv5 = batch_norm(conv5, is_training) conv5 = tf.nn.relu(conv5) # 池化层5 pool5 = tf.nn.avg_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # 第六层全连接 reshape = tf.reshape(pool5, [-1, 6 * 6 * 256]) #fc1 = tf.matmul(reshape, W_conv['fc1']) fc1 = tf.add(tf.matmul(reshape, W_conv['fc1']), b_conv['fc1']) #fc1 = tf.layers.batch_normalization(fc1, training=is_training) fc1 = batch_norm(fc1, is_training, False) fc1 = tf.nn.relu(fc1) #fc1 = tf.nn.dropout(fc1, 0.5) # 第七层全连接 #fc2 = tf.matmul(fc1, W_conv['fc2']) fc2 = tf.add(tf.matmul(fc1, W_conv['fc2']), b_conv['fc2']) #fc2 = tf.layers.batch_normalization(fc2, training=is_training) fc2 = batch_norm(fc2, is_training, False) fc2 = tf.nn.relu(fc2) #fc2 = tf.nn.dropout(fc2, 0.5) # 第八层全连接(分类层) yop = tf.add(tf.matmul(fc2, W_conv['fc3']), b_conv['fc3']) # 损失函数 #y = tf.stop_gradient(y) loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=yop, labels=y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) #with tf.control_dependencies(update_ops): # 保证train_op在update_ops执行之后再执行。 #train_op = optimizer.minimize(loss) # 评估模型 correct_predict = tf.nn.in_top_k(yop, y, 1) accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32)) init = tf.global_variables_initializer() def onehot(labels): # 独热编码表示数据 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 # python迭代方法,将每一行对应个置1 return onehot_labels save_model = './/model//my-model.ckpt' # 模型训练 def train(epoch): with tf.Session() as sess: sess.run(init) saver = tf.train.Saver(var_list=tf.global_variables()) c = [] b = [] max_acc = 0 start_time = time.time() step = 0 global dataset dataset = dataset.get_next() for i in range(epoch): step = i image, labels = sess.run(dataset) sess.run(optimizer, feed_dict={x: image, y: labels, is_training: True}) # 训练一次 #if i % 5 == 0: loss_record = sess.run(loss, feed_dict={x: image, y: labels, is_training: True}) # 记录一次 #predict = sess.run(yop, feed_dict={x: image, y: labels, is_training: True}) acc = sess.run(accuracy, feed_dict={x: image, y: labels, is_training: True}) print("step:%d, now the loss is %f" % (step, loss_record)) #print(predict[0]) print("acc : %f" % acc) c.append(loss_record) b.append(acc) end_time = time.time() print('time:', (end_time - start_time)) start_time = end_time print('-----------%d opench is finished ------------' % (i / 5)) #if acc > max_acc: # max_acc = acc # saver.save(sess, save_model, global_step=i + 1) print('Optimization Finished!') #saver.save(sess, save_model) print('Model Save Finished!') plt.plot(c) plt.plot(b) plt.xlabel('iter') plt.ylabel('loss') plt.title('lr=%f, ti=%d, bs=%d' % (learning_rate, training_iters, batch_size)) plt.tight_layout() plt.show() X_train, y_train = get_file("D://cat_and_dog//cat_dog_train//cat_dog") # 返回为文件地址 dataset = get_batch(X_train, y_train, 100) train(100) ``` 数据文件夹为猫狗大战那个25000个图片的文件,不加入正则表达层的时候训练集loss会下降,但是acc维持不变,加入__batch norm__或者__tf.layers.batch__normalization 训练集和验证机的loss都不收敛了

'Datasets' object has no attribute 'train_step'

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import mnist_forward import os BATAH_SIZE = 200 LEARNING_RATE_BASE = 0.1 LEARNING_RATE_DECAY = 0.99 REGULARIZER = 0.0001 STEPS = 50000 MOVING_AVERAGE_DECAY = 0.99 MODEL_SAVE_PATH = "./model/" MODEL_NAME = "mnist_model" def backward(mnist): x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE]) y = mnist_forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.arg_max(y_, 1)) cem = tf.reduce_mean(ce) loss = cem + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATAH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver() with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): xs, ys = mnist.train_step.next_batch(BATAH_SIZE) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys}) if i % 1000 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) def main(): mnist = input_data.read_data_sets("./data/", one_hot=True) backward(mnist) if __name__ == '__main__': main() 运行程序后报错: File "C:/Users/98382/PycharmProjects/minst/mnist_backward.py", line 54, in <module> main() File "C:/Users/98382/PycharmProjects/minst/mnist_backward.py", line 51, in main backward(mnist) File "C:/Users/98382/PycharmProjects/minst/mnist_backward.py", line 43, in backward xs, ys = mnist.train_step.next_batch(BATAH_SIZE) AttributeError: 'Datasets' object has no attribute 'train_step'

关于Tensorflow的minst数字识别出现name XXX is not defined的问题

``` def train(mnist): x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input') weights1 = tf.Variable( tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1)) biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE])) weights2 = tf.Variable( tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1)) biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) #计算向前传播y的输出值,此处首次向前传播计算,不必使用滑动平均值来进行权值优化,固填为None y = inference(x, None, weights1, biases1, weights2, biases2) #存储训练轮数,变量为不可训练的变量 global_step = tf.Variable(0, trainable=False) #给定滑动平均衰减率和训练轮数的变量,初始化滑动平均类,给定训练轮数可以加快早期 #变量训练速度 variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) #创建指数移动平均类 #在所有代表神经网络参数的变量上使用滑动平均,这个集合元素是所有没有指定 #trainable = False 的参数 variable_averages_op = variable_averages.apply( tf.trainable_variables()) #将上类作用于当前所有可训练变量 #计算使用滑动平均的Y值,这里调用之前定义的inference函数,获取参数 average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2) #计算交叉熵,作为刻画真实值y与预测值y_之间的差距 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) #计算当前BATCH中所有样例的交叉熵平均值 corss_entropy_mean = tf.reduce_mean(cross_entropy) #计算L2正则化损失函数 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) #计算模型正则化损失 regularization = regularizer(weights1) + regularizer(weights2) #总损失为交叉熵与正则化的和 loss = cross_entropy_mean + regularization #衰减学习率 global learning_rate learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) #最小化loss train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) with tf.control_dependencies([train_step, Variables_averages_op]): train_op = tf.no_op(name='train') ``` 可以看到在前面的train函数中定义了learning_rate局部变量,但是在外部调用时出现变量未定义的报错,我定义全局变量也没有用![图片说明](https://img-ask.csdn.net/upload/201807/02/1530499378_484353.png)

求解报错TypeError: slice indices must be integers or None or have an __index__ method

运行环境 pycharm2019.2.3 python 3.7 TensorFlow 2.0 代码如下 ``` import tensorflow as tf import numpy as np class DataLoader(): def __init__(self): path = tf.keras.utils.get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt') with open(path, encoding='utf-8') as f: self.raw_text = f.read().lower() self.chars = sorted(list(set(self.raw_text))) self.char_indices = dict((c, i) for i, c in enumerate(self.chars)) self.indices_char = dict((i, c) for i, c in enumerate(self.chars)) self.text = [self.char_indices[c] for c in self.raw_text] def get_batch(self, seq_length, batch_size): seq = [] next_char = [] for i in range(batch_size): index = np.random.randint(0, len(self.text) - seq_length) seq.append(self.text[index:index + seq_length]) next_char.append(self.text[index + seq_length]) return np.array(seq), np.array(next_char) # [batch_size, seq_length], [num_batch] class RNN(tf.keras.Model): def __init__(self, num_chars, batch_size, seq_length): super().__init__() self.num_chars = num_chars self.seq_length = seq_length self.batch_size = batch_size self.cell = tf.keras.layers.LSTMCell(units=256) self.dense = tf.keras.layers.Dense(units=self.num_chars) def call(self, inputs, from_logits=False): inputs = tf.one_hot(inputs, depth=self.num_chars) # [batch_size, seq_length, num_chars] state = self.cell.get_initial_state(batch_size=self.batch_size, dtype=tf.float32) for t in range(self.seq_length): output, state = self.cell(inputs[:, t, :], state) logits = self.dense(output) if from_logits: return logits else: return tf.nn.softmax(logits) num_batches = 10 seq_length = 40 batch_size = 50 learning_rate = 1e-3 data_loader = DataLoader() model = RNN(num_chars=len(data_loader.chars), batch_size=batch_size, seq_length=seq_length) optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate) for batch_index in range(num_batches): X, y = data_loader.get_batch(seq_length, batch_size) with tf.GradientTape() as tape: y_pred = model(X) loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred) loss = tf.reduce_mean(loss) print("batch %d: loss %f" % (batch_index, loss.numpy())) grads = tape.gradient(loss, model.variables) optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables)) def predict(self, inputs, temperature=1.): batch_size, _ = tf.shape(inputs) logits = self(inputs, from_logits=True) prob = tf.nn.softmax(logits / temperature).numpy() return np.array([np.random.choice(self.num_chars, p=prob[i, :]) for i in range(batch_size.numpy())]) X_, _ = data_loader.get_batch(seq_length, 1) for diversity in [0.2, 0.5, 1.0, 1.2]: X = X_ print("diversity %f:" % diversity) for t in range(400): y_pred = model.predict(X, diversity) print(data_loader.indices_char[y_pred[0]], end='', flush=True) X = np.concatenate([X[:, 1:], np.expand_dims(y_pred, axis=1)], axis=-1) print("\n") ``` 报错: ``` Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] on win32 runfile('F:/pyth/pj3/study3.py', wdir='F:/pyth/pj3') batch 0: loss 4.041161 batch 1: loss 4.026710 batch 2: loss 4.005230 batch 3: loss 3.983728 batch 4: loss 3.920999 batch 5: loss 3.864793 batch 6: loss 3.644211 batch 7: loss 3.375458 batch 8: loss 3.620051 batch 9: loss 3.382381 diversity 0.200000: Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "F:/pyth/pj3/study3.py", line 97, in <module> y_pred = model.predict(X, diversity) File "D:\ProgramData\Anaconda3\envs\kingtf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 909, in predict use_multiprocessing=use_multiprocessing) File "D:\ProgramData\Anaconda3\envs\kingtf2\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 722, in predict callbacks=callbacks) File "D:\ProgramData\Anaconda3\envs\kingtf2\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 362, in model_iteration batch_ids = index_array[batch_start:batch_end] TypeError: slice indices must be integers or None or have an __index__ method ``` 可能有问题的地方: ``` for diversity in [0.2, 0.5, 1.0, 1.2]: X = X_ print("diversity %f:" % diversity) for t in range(400): y_pred = model.predict(X, diversity) print(data_loader.indices_char[y_pred[0]], end='', flush=True) X = np.concatenate([X[:, 1:], np.expand_dims(y_pred, axis=1)], axis=-1) print("\n") ``` ``` def predict(self, inputs, temperature=1.): batch_size, _ = tf.shape(inputs) logits = self(inputs, from_logits=True) prob = tf.nn.softmax(logits / temperature).numpy() return np.array([np.random.choice(self.num_chars, p=prob[i, :]) for i in range(batch_size.numpy())]) ```

Linux+pytorch下运行报错RuntimeError: PyTorch was compiled without NumPy support

我在尝试实现Github上开源的代码[Relation-Shape-CNN](https://github.com/Yochengliu/Relation-Shape-CNN ""),运行报错RuntimeError: PyTorch was compiled without NumPy support train_cls.py:36: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. config = yaml.load(f) ************************** [workers]: 4 [num_points]: 1024 [num_classes]: 40 [batch_size]: 32 [base_lr]: 0.001 [lr_clip]: 1e-05 [lr_decay]: 0.7 [decay_step]: 21 [epochs]: 200 [weight_decay]: 0 [bn_momentum]: 0.9 [bnm_clip]: 0.01 [bn_decay]: 0.5 [evaluate]: 1 [val_freq_epoch]: 0.5 [print_freq_iter]: 20 [input_channels]: 0 [relation_prior]: 1 [checkpoint]: [save_path]: cls [data_root]: /media/lab/16DE307A392D4AED/zs ************************** /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:113: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_. init(xyz_raising.weight) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:115: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(xyz_raising.bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:122: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_. init(mapping_func1.weight) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:123: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_. init(mapping_func2.weight) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:125: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(mapping_func1.bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:126: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(mapping_func2.bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:131: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_. init(cr_mapping.weight) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pointnet2_modules.py:132: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(cr_mapping.bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pytorch_utils/pytorch_utils.py:153: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_. init(self.conv_avg.weight) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pytorch_utils/pytorch_utils.py:155: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(self.conv_avg.bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pytorch_utils/pytorch_utils.py:201: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(self[0].weight, 1.0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pytorch_utils/pytorch_utils.py:202: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(self[0].bias, 0) /media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/models/../utils/pytorch_utils/pytorch_utils.py:400: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_. nn.init.constant(fc.bias, 0) Traceback (most recent call last): File "train_cls.py", line 167, in <module> main() File "train_cls.py", line 91, in main train(train_dataloader, test_dataloader, model, criterion, optimizer, lr_scheduler, bnm_scheduler, args, num_batch) File "train_cls.py", line 101, in train for i, data in enumerate(train_dataloader, 0): File "/home/lab/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 336, in __next__ return self._process_next_batch(batch) File "/home/lab/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 357, in _process_next_batch raise batch.exc_type(batch.exc_msg) RuntimeError: Traceback (most recent call last): File "/home/lab/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop samples = collate_fn([dataset[i] for i in batch_indices]) File "/home/lab/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 106, in <listcomp> samples = collate_fn([dataset[i] for i in batch_indices]) File "/media/lab/16DE307A392D4AED/zs/Relation-Shape-CNN/data/ModelNet40Loader.py", line 55, in __getitem__ label = torch.from_numpy(self.labels[idx]).type(torch.LongTensor) RuntimeError: PyTorch was compiled without NumPy support 请大神解答!!!

tensorflow简单的手写数字识别矩阵相乘时出现问题

跟着教程抄的程序为什么运行不了呢 抄下来的程序 ``` #载入数据集 mnist=input_data.read_data_sets("MNIST_data/",one_hot=True) #每个批次大小 batch_size=100 #计算一共有多少批次 n_batch=mnist.train.num_examples//batch_size #定义有两个placeholder x=tf.placeholder(tf.float32,[None,784]) y=tf.placeholder(tf.float32,[None,10]) #创建一个简单的神经网络 W=tf.Variable(tf.zeros([784.10])) b=tf.Variable(tf.zeros([10])) prediction=tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数 loss=tf.reduce_mean(tf.square(y-perdiction)) #梯度下降法 train_step=tf.train.GradientOptimizer(0.2).minimize(loss) #初始化变量 init=tf.global_variable_initializer() #结果存放在一个布尔型列表中 correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#返回一维张量中最大值的位置 #求准确率 accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Sessio() as sess: sess.run(init) for epoch in range(21): for batch in range(n_batch): batch_xs,batchys=mnist.train.next_batch(batch_size) sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc=sess.run(accury,feed_dict={x:mnis.test.images,y:mnist.test.labels}) print("Iter"+str(epoch)+",Testing Accuracy"+str(acc)) ``` 给我的警告 ``` Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz --------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs) 1658 try: -> 1659 c_op = c_api.TF_FinishOperation(op_desc) 1660 except errors.InvalidArgumentError as e: InvalidArgumentError: Shape must be rank 2 but is rank 1 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [784]. During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-5-5e22f4dd85e3> in <module>() 14 W=tf.Variable(tf.zeros([784.10])) 15 b=tf.Variable(tf.zeros([10])) ---> 16 prediction=tf.nn.softmax(tf.matmul(x,W)+b) 17 18 #二次代价函数 C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, name) 2453 else: 2454 return gen_math_ops.mat_mul( -> 2455 a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name) 2456 2457 C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in mat_mul(a, b, transpose_a, transpose_b, name) 5627 _, _, _op = _op_def_lib._apply_op_helper( 5628 "MatMul", a=a, b=b, transpose_a=transpose_a, transpose_b=transpose_b, -> 5629 name=name) 5630 _result = _op.outputs[:] 5631 _inputs_flat = _op.inputs C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 786 op = g.create_op(op_type_name, inputs, output_types, name=scope, 787 input_types=input_types, attrs=attr_protos, --> 788 op_def=op_def) 789 return output_structure, op_def.is_stateful, op 790 C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs) 505 'in a future version' if date is None else ('after %s' % date), 506 instructions) --> 507 return func(*args, **kwargs) 508 509 doc = _add_deprecated_arg_notice_to_docstring( C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in create_op(***failed resolving arguments***) 3298 input_types=input_types, 3299 original_op=self._default_original_op, -> 3300 op_def=op_def) 3301 self._create_op_helper(ret, compute_device=compute_device) 3302 return ret C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def) 1821 op_def, inputs, node_def.attr) 1822 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs, -> 1823 control_input_ops) 1824 1825 # Initialize self._outputs. C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs) 1660 except errors.InvalidArgumentError as e: 1661 # Convert to ValueError for backwards compatibility. -> 1662 raise ValueError(str(e)) 1663 1664 return c_op ValueError: Shape must be rank 2 but is rank 1 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [784]. ``` 跪求大佬解答

TensorFlow的Keras如何使用Dataset作为数据输入?

当我把dataset作为输入数据是总会报出如下错误,尽管我已经在数据解析那里reshape了图片大小为(512,512,1),请问该如何修改? ``` ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) ``` **图片大小定义** ``` import tensorflow as tf from tensorflow import keras IMG_HEIGHT = 512 IMG_WIDTH = 512 IMG_CHANNELS = 1 IMG_PIXELS = IMG_CHANNELS * IMG_HEIGHT * IMG_WIDTH ``` **解析函数** ``` def parser(record): features = tf.parse_single_example(record, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([23], tf.int64) }) image = tf.decode_raw(features['image_raw'], tf.uint8) label = tf.cast(features['label'], tf.int32) image.set_shape([IMG_PIXELS]) image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS]) image = tf.cast(image, tf.float32) return image, label ``` **模型构建** ``` dataset = tf.data.TFRecordDataset([TFRECORD_PATH]) dataset.map(parser) dataset = dataset.repeat(10*10).batch(10) model = keras.Sequential([ keras.layers.Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu', input_shape=(512, 512, 1)), keras.layers.MaxPool2D(pool_size=(2, 2)), keras.layers.Dropout(0.25), keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'), keras.layers.MaxPool2D(pool_size=(2, 2)), keras.layers.Dropout(0.25), keras.layers.Flatten(), keras.layers.Dense(128, activation='relu'), keras.layers.Dropout(0.25), keras.layers.Dense(23, activation='softmax') ]) model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.sparse_categorical_crossentropy, metrics=[tf.keras.metrics.categorical_accuracy]) model.fit(dataset.make_one_shot_iterator(), epochs=10, steps_per_epoch=10) ```

ValueError: invalid literal for int() with base 10: 'aer'

#coding=utf-8 #Version:python3.6.0 #Tools:Pycharm 2017.3.2 import numpy as np import tensorflow as tf import re TRAIN_PATH="data/ptb.train.txt" EVAL_PATH="data/ptb.valid.txt" TEST_PATH="data/ptb.test.txt" HIDDEN_SIZE=300 NUM_LAYERS=2 VOCAB_SIZE=10000 TRAIN_BATCH_SIZE=20 TRAIN_NUM_STEP=35 EVAL_BATCH_SIZE=1 EVAL_NUM_STEP=1 NUM_EPOCH=5 LSTM_KEEP_PROB=0.9 EMBEDDING_KEEP_PROB=0.9 MAX_GRED_NORM=5 SHARE_EMB_AND_SOFTMAX=True class PTBModel(object): def __init__(self,is_training,batch_size,num_steps): self.batch_size=batch_size self.num_steps=num_steps self.input_data=tf.placeholder(tf.int32,[batch_size,num_steps]) self.targets=tf.placeholder(tf.int32,[batch_size,num_steps]) dropout_keep_prob=LSTM_KEEP_PROB if is_training else 1.0 lstm_cells=[ tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE), output_keep_prob=dropout_keep_prob) for _ in range (NUM_LAYERS)] cell=tf.nn.rnn_cell.MultiRNNCell(lstm_cells) self.initial_state=cell.zero_state(batch_size,tf.float32) embedding=tf.get_variable("embedding",[VOCAB_SIZE,HIDDEN_SIZE]) inputs=tf.nn.embedding_lookup(embedding,self.input_data) if is_training: inputs=tf.nn.dropout(inputs,EMBEDDING_KEEP_PROB) outputs=[] state=self.initial_state with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step>0:tf.get_variable_scope().reuse_variables() cell_output,state=cell(inputs[:,time_step,:],state) outputs.append(cell_output) # 把输出队列展开成[batch,hidden_size*num_steps]的形状,然后再reshape成[batch*numsteps,hidden_size]的形状 output=tf.reshape(tf.concat(outputs,1),[-1,HIDDEN_SIZE]) if SHARE_EMB_AND_SOFTMAX: weight=tf.transpose(embedding) else: weight=tf.get_variable("weight",[HIDDEN_SIZE,VOCAB_SIZE]) bias=tf.get_variable("bias",[VOCAB_SIZE]) logits=tf.matmul(output,weight)+bias loss=tf.nn.sparse_softmax_cross_entropy_with_logits( labels=tf.reshape(self.targets,[-1]), logits=logits ) self.cost=tf.reduce_sum(loss)/batch_size self.final_state=state # 只在训练模型时定义反向传播操作 if not is_training:return trainable_variables=tf.trainable_variables() #控制梯度大小 grads,_=tf.clip_by_global_norm( tf.gradients(self.cost,trainable_variables),MAX_GRED_NORM) # 定义优化方法 optimizer=tf.train.GradientDescentOptimizer(learning_rate=1.0) # zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。 #定义训练步骤 self.train_op=optimizer.apply_gradients( zip(grads,trainable_variables)) def run_epoch(session,model,batches,train_op,output_log,step): total_costs=0.0 iters=0 state=session.run(model.initial_state) for x,y in batches: cost,state,_=session.run( [model.cost,model.final_state,train_op], {model.input_data:x,model.targets:y, model.initial_state:state} ) total_costs+=cost iters+=model.num_steps # 只有在训练时输出日志 if output_log and step %100==0: print("After %d steps,perplexity is %.3f"%( step,np.exp(total_costs/iters) )) step +=1 return step,np.exp(total_costs/iters) # 从文件中读取数据,并返回包含单词编号的数组 def read_data(file_path): with open(file_path,"r") as fin: id_string=" ".join([line.strip() for line in fin.readlines()]) id_list=[int(w) for w in id_string.split()] # 将读取的单词编号转为整数 return id_list def make_batches(id_list,batch_size,num_step): # 计算总的batch数量,每个batch包含的单词数量是batch_size*num_step try: num_batches=(len(id_list)-1)/(batch_size*num_step) data=np.array(id_list[:num_batches*batch_size*num_step]) data=np.reshape(data,[batch_size,num_batches*num_step]) data_batches=np.split(data,num_batches,axis=1) label=np.array(id_list[1:num_batches*batch_size*num_step+1]) label=np.reshape(label,[batch_size,num_batches*num_step]) label_batches=np.split(label,num_batches,axis=1) return list(zip(data_batches,label_batches)) def main(): # 定义初始化函数 intializer=tf.random_uniform_initializer(-0.05,0.05) with tf.variable_scope("language_model",reuse=None,initializer=intializer): train_model=PTBModel(True,TRAIN_BATCH_SIZE,TRAIN_NUM_STEP) with tf.variable_scope("language_model",reuse=True,initializer=intializer): eval_model=PTBModel(False,EVAL_BATCH_SIZE,EVAL_NUM_STEP) with tf.Session() as session: tf.global_variables_initializer().run() train_batches=make_batches(read_data(TRAIN_PATH),TRAIN_BATCH_SIZE,TRAIN_NUM_STEP) eval_batches=make_batches(read_data(EVAL_PATH),EVAL_BATCH_SIZE,EVAL_NUM_STEP) test_batches=make_batches(read_data(TEST_PATH),EVAL_BATCH_SIZE,EVAL_NUM_STEP) step=0 for i in range(NUM_EPOCH): print("In iteration:%d" % (i+1)) step,train_pplx=run_epoch(session,train_model,train_batches,train_model.train_op,True,step) print("Epoch:%d Train perplexity:%.3f"%(i+1,train_pplx)) _,eval_pplx=run_epoch(session,eval_model,eval_batches,tf.no_op,False,0) print("Epoch:%d Eval perplexity:%.3f"%(i+1,eval_pplx)) _,test_pplx=run_epoch(session,eval_model,test_batches,tf.no_op(),False,0) print("Test perplexity:%.3f"% test_pplx) if __name__ == '__main__': main()

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 ``` 以上的模型代码,请求各位大神帮我看看,为什么出现这样的结果?

TypeError: 'FileWriter' object is not callable

import tensorflow as tf import tensorlayer as tl import numpy as np class CNNEnv: def __init__(self): # The data, shuffled and split between train and test sets self.x_train, self.y_train, self.x_test, self.y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) # Reorder dimensions for tensorflow self.mean = np.mean(self.x_train, axis=0, keepdims=True) self.std = np.std(self.x_train) self.x_train = (self.x_train - self.mean) / self.std self.x_test = (self.x_test - self.mean) / self.std print('x_train shape:', self.x_train.shape) print('x_test shape:', self.x_test.shape) print('y_train shape:', self.y_train.shape) print('y_test shape:', self.y_test.shape) # For generator self.num_examples = self.x_train.shape[0] self.index_in_epoch = 0 self.epochs_completed = 0 # For wide resnets self.blocks_per_group = 4 self.widening_factor = 4 # Basic info self.batch_num = 64 self.img_row = 32 self.img_col = 32 self.img_channels = 3 self.nb_classes = 10 def next_batch(self, batch_size): """Return the next `batch_size` examples from this data set.""" self.batch_size = batch_size start = self.index_in_epoch self.index_in_epoch += self.batch_size if self.index_in_epoch > self.num_examples: # Finished epoch self.epochs_completed += 1 # Shuffle the data perm = np.arange(self.num_examples) np.random.shuffle(perm) self.x_train = self.x_train[perm] self.y_train = self.y_train[perm] # Start next epoch start = 0 self.index_in_epoch = self.batch_size assert self.batch_size <= self.num_examples end = self.index_in_epoch return self.x_train[start:end], self.y_train[start:end] def reset(self, first): self.first = first if self.first is True: self.sess.close() config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.InteractiveSession(config=config) def step(self): def zero_pad_channels(x, pad=0): """ Function for Lambda layer """ pattern = [[0, 0], [0, 0], [0, 0], [pad - pad // 2, pad // 2]] return tf.pad(x, pattern) def residual_block(x, count, nb_filters=16, subsample_factor=1): prev_nb_channels = x.outputs.get_shape().as_list()[3] if subsample_factor > 1: subsample = [1, subsample_factor, subsample_factor, 1] # shortcut: subsample + zero-pad channel dim name_pool = 'pool_layer' + str(count) shortcut = tl.layers.PoolLayer(x, ksize=subsample, strides=subsample, padding='VALID', pool=tf.nn.avg_pool, name=name_pool) else: subsample = [1, 1, 1, 1] # shortcut: identity shortcut = x if nb_filters > prev_nb_channels: name_lambda = 'lambda_layer' + str(count) shortcut = tl.layers.LambdaLayer( shortcut, zero_pad_channels, fn_args={'pad': nb_filters - prev_nb_channels}, name=name_lambda) name_norm = 'norm' + str(count) y = tl.layers.BatchNormLayer(x, decay=0.999, epsilon=1e-05, is_train=True, name=name_norm) name_conv = 'conv_layer' + str(count) y = tl.layers.Conv2dLayer(y, act=tf.nn.relu, shape=[3, 3, prev_nb_channels, nb_filters], strides=subsample, padding='SAME', name=name_conv) name_norm_2 = 'norm_second' + str(count) y = tl.layers.BatchNormLayer(y, decay=0.999, epsilon=1e-05, is_train=True, name=name_norm_2) prev_input_channels = y.outputs.get_shape().as_list()[3] name_conv_2 = 'conv_layer_second' + str(count) y = tl.layers.Conv2dLayer(y, act=tf.nn.relu, shape=[3, 3, prev_input_channels, nb_filters], strides=[1, 1, 1, 1], padding='SAME', name=name_conv_2) name_merge = 'merge' + str(count) out = tl.layers.ElementwiseLayer([y, shortcut], combine_fn=tf.add, name=name_merge) return out # Placeholders learning_rate = tf.placeholder(tf.float32) img = tf.placeholder(tf.float32, shape=[self.batch_num, 32, 32, 3]) labels = tf.placeholder(tf.int32, shape=[self.batch_num, ]) x = tl.layers.InputLayer(img, name='input_layer') x = tl.layers.Conv2dLayer(x, act=tf.nn.relu, shape=[3, 3, 3, 16], strides=[1, 1, 1, 1], padding='SAME', name='cnn_layer_first') for i in range(0, self.blocks_per_group): nb_filters = 16 * self.widening_factor count = i x = residual_block(x, count, nb_filters=nb_filters, subsample_factor=1) for i in range(0, self.blocks_per_group): nb_filters = 32 * self.widening_factor if i == 0: subsample_factor = 2 else: subsample_factor = 1 count = i + self.blocks_per_group x = residual_block(x, count, nb_filters=nb_filters, subsample_factor=subsample_factor) for i in range(0, self.blocks_per_group): nb_filters = 64 * self.widening_factor if i == 0: subsample_factor = 2 else: subsample_factor = 1 count = i + 2*self.blocks_per_group x = residual_block(x, count, nb_filters=nb_filters, subsample_factor=subsample_factor) x = tl.layers.BatchNormLayer(x, decay=0.999, epsilon=1e-05, is_train=True, name='norm_last') x = tl.layers.PoolLayer(x, ksize=[1, 8, 8, 1], strides=[1, 8, 8, 1], padding='VALID', pool=tf.nn.avg_pool, name='pool_last') x = tl.layers.FlattenLayer(x, name='flatten') x = tl.layers.DenseLayer(x, n_units=self.nb_classes, act=tf.identity, name='fc') output = x.outputs ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(output, labels)) cost = ce tf.summary.histogram('cost',cost) correct_prediction = tf.equal(tf.cast(tf.argmax(output, 1), tf.int32), labels) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.histogram('acc',acc) train_params = x.all_params train_op = tf.train.GradientDescentOptimizer( learning_rate, use_locking=False).minimize(cost, var_list=train_params) merged = tf.summary.merge_all() writer1 = tf.summary.FileWriter('train/',self.sess.graph) #先执行 self.sess.run(tf.global_variables_initializer()) tf.global_variables_initializer() for i in range(10): batch = self.next_batch(self.batch_num) feed_dict = {img: batch[0], labels: batch[1], learning_rate: 0.01} feed_dict.update(x.all_drop) tp, l, ac,me1 = self.sess.run([train_op, cost, acc,merged], feed_dict=feed_dict) writer1(me1,i) print('loss', l) print('acc', ac) writer1.close() a = CNNEnv() a.reset(first=False) a.step()

用tensorflow做机器翻译时训练代码有问题

``` # -*- coding:UTF-8 -*- import tensorflow as tf src_path = 'D:/Python37/untitled1/train.tags.en-zh.en.deletehtml' trg_path = 'D:/Python37/untitled1/train.tags.en-zh.zh.deletehtml' SRC_TRAIN_DATA = 'D:/Python37/untitled1/train.tags.en-zh.en.deletehtml.segment' # 源语言输入文件 TRG_TRAIN_DATA = 'D:/Python37/untitled1/train.tags.en-zh.zh.deletehtml.segment' # 目标语言输入文件 CHECKPOINT_PATH = './model/seq2seq_ckpt' # checkpoint保存路径 HIDDEN_SIZE = 1024 # LSTM的隐藏层规模 NUM_LAYERS = 2 # 深层循环神经网络中LSTM结构的层数 SRC_VOCAB_SIZE = 10000 # 源语言词汇表大小 TRG_VOCAB_SIZE = 4000 # 目标语言词汇表大小 BATCH_SIZE = 100 # 训练数据batch的大小 NUM_EPOCH = 5 # 使用训练数据的轮数 KEEP_PROB = 0.8 # 节点不被dropout的概率 MAX_GRAD_NORM = 5 # 用于控制梯度膨胀的梯度大小上限 SHARE_EMB_AND_SOFTMAX = True # 在softmax层和词向量层之间共享参数 MAX_LEN = 50 # 限定句子的最大单词数量 SOS_ID = 1 # 目标语言词汇表中<sos>的ID """ function: 数据batching,产生最后输入数据格式 Parameters: file_path-数据路径 Returns: dataset- 每个句子-对应的长度组成的TextLineDataset类的数据集对应的张量 """ def MakeDataset(file_path): dataset = tf.data.TextLineDataset(file_path) # map(function, sequence[, sequence, ...]) -> list # 通过定义可以看到,这个函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。 # function可以理解为是一个一对一或多对一函数,map的作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的list。 # lambda argument_list: expression # 其中lambda是Python预留的关键字,argument_list和expression由用户自定义 # argument_list参数列表, expression 为函数表达式 # 根据空格将单词编号切分开并放入一个一维向量 dataset = dataset.map(lambda string: tf.string_split([string]).values) # 将字符串形式的单词编号转化为整数 dataset = dataset.map(lambda string: tf.string_to_number(string, tf.int32)) # 统计每个句子的单词数量,并与句子内容一起放入Dataset dataset = dataset.map(lambda x: (x, tf.size(x))) return dataset """ function: 从源语言文件src_path和目标语言文件trg_path中分别读取数据,并进行填充和batching操作 Parameters: src_path-源语言,即被翻译的语言,英语. trg_path-目标语言,翻译之后的语言,汉语. batch_size-batch的大小 Returns: dataset- 每个句子-对应的长度 组成的TextLineDataset类的数据集 """ def MakeSrcTrgDataset(src_path, trg_path, batch_size): # 首先分别读取源语言数据和目标语言数据 src_data = MakeDataset(src_path) trg_data = MakeDataset(trg_path) # 通过zip操作将两个Dataset合并为一个Dataset,现在每个Dataset中每一项数据ds由4个张量组成 # ds[0][0]是源句子 # ds[0][1]是源句子长度 # ds[1][0]是目标句子 # ds[1][1]是目标句子长度 #https://blog.csdn.net/qq_32458499/article/details/78856530这篇博客看一下可以细致了解一下Dataset这个库,以及.map和.zip的用法 dataset = tf.data.Dataset.zip((src_data, trg_data)) # 删除内容为空(只包含<eos>)的句子和长度过长的句子 def FilterLength(src_tuple, trg_tuple): ((src_input, src_len), (trg_label, trg_len)) = (src_tuple, trg_tuple) # tf.logical_and 相当于集合中的and做法,后面两个都为true最终结果才会为true,否则为false # tf.greater Returns the truth value of (x > y),所以以下所说的是句子长度必须得大于一也就是不能为空的句子 # tf.less_equal Returns the truth value of (x <= y),所以所说的是长度要小于最长长度 src_len_ok = tf.logical_and(tf.greater(src_len, 1), tf.less_equal(src_len, MAX_LEN)) trg_len_ok = tf.logical_and(tf.greater(trg_len, 1), tf.less_equal(trg_len, MAX_LEN)) return tf.logical_and(src_len_ok, trg_len_ok) #两个都满足才返回true # filter接收一个函数Func并将该函数作用于dataset的每个元素,根据返回值True或False保留或丢弃该元素,True保留该元素,False丢弃该元素 # 最后得到的就是去掉空句子和过长的句子的数据集 dataset = dataset.filter(FilterLength) # 解码器需要两种格式的目标句子: # 1.解码器的输入(trg_input), 形式如同'<sos> X Y Z' # 2.解码器的目标输出(trg_label), 形式如同'X Y Z <eos>' # 上面从文件中读到的目标句子是'X Y Z <eos>'的形式,我们需要从中生成'<sos> X Y Z'形式并加入到Dataset # 编码器只有输入,没有输出,而解码器有输入也有输出,输入为<sos>+(除去最后一位eos的label列表) # 例如train.en最后都为2,id为2就是eos def MakeTrgInput(src_tuple, trg_tuple): ((src_input, src_len), (trg_label, trg_len)) = (src_tuple, trg_tuple) # tf.concat用法 https://blog.csdn.net/qq_33431368/article/details/79429295 trg_input = tf.concat([[SOS_ID], trg_label[:-1]], axis=0) return ((src_input, src_len), (trg_input, trg_label, trg_len)) dataset = dataset.map(MakeTrgInput) # 随机打乱训练数据 dataset = dataset.shuffle(10000) # 规定填充后的输出的数据维度 padded_shapes = ( (tf.TensorShape([None]), # 源句子是长度未知的向量 tf.TensorShape([])), # 源句子长度是单个数字 (tf.TensorShape([None]), # 目标句子(解码器输入)是长度未知的向量 tf.TensorShape([None]), # 目标句子(解码器目标输出)是长度未知的向量 tf.TensorShape([])) # 目标句子长度(输出)是单个数字 ) # 调用padded_batch方法进行padding 和 batching操作 batched_dataset = dataset.padded_batch(batch_size, padded_shapes) return batched_dataset """ function: seq2seq模型 Parameters: Returns: """ class NMTModel(object): """ function: 模型初始化 Parameters: Returns: """ def __init__(self): # 定义编码器和解码器所使用的LSTM结构 self.enc_cell = tf.nn.rnn_cell.MultiRNNCell( [tf.nn.rnn_cell.LSTMCell(HIDDEN_SIZE) for _ in range(NUM_LAYERS)]) self.dec_cell = tf.nn.rnn_cell.MultiRNNCell( [tf.nn.rnn_cell.LSTMCell(HIDDEN_SIZE) for _ in range(NUM_LAYERS)]) # 为源语言和目标语言分别定义词向量 self.src_embedding = tf.get_variable('src_emb', [SRC_VOCAB_SIZE, HIDDEN_SIZE]) self.trg_embedding = tf.get_variable('trg_emb', [TRG_VOCAB_SIZE, HIDDEN_SIZE]) # 定义softmax层的变量 if SHARE_EMB_AND_SOFTMAX: self.softmax_weight = tf.transpose(self.trg_embedding) else: self.softmax_weight = tf.get_variable('weight', [HIDDEN_SIZE, TRG_VOCAB_SIZE]) self.softmax_bias = tf.get_variable('softmax_loss', [TRG_VOCAB_SIZE]) """ function: 在forward函数中定义模型的前向计算图 Parameters:   MakeSrcTrgDataset函数产生的五种张量如下(全部为张量) src_input: 编码器输入(源数据) src_size : 输入大小 trg_input:解码器输入(目标数据) trg_label:解码器输出(目标数据) trg_size: 输出大小 Returns: """ def forward(self, src_input, src_size, trg_input, trg_label, trg_size): batch_size = tf.shape(src_input)[0] # 将输入和输出单词转为词向量(rnn中输入数据都要转换成词向量) # 相当于input中的每个id对应的embedding中的向量转换 src_emb = tf.nn.embedding_lookup(self.src_embedding, src_input) trg_emb = tf.nn.embedding_lookup(self.trg_embedding, trg_input) # 在词向量上进行dropout src_emb = tf.nn.dropout(src_emb, KEEP_PROB) trg_emb = tf.nn.dropout(trg_emb, KEEP_PROB) # 使用dynamic_rnn构造编码器 # 编码器读取源句子每个位置的词向量,输出最后一步的隐藏状态enc_state # 因为编码器是一个双层LSTM,因此enc_state是一个包含两个LSTMStateTuple类的tuple, # 每个LSTMStateTuple对应编码器中一层的状态 # enc_outputs是顶层LSTM在每一步的输出,它的维度是[batch_size, max_time, HIDDEN_SIZE] # seq2seq模型中不需要用到enc_outputs,而attention模型会用到它 with tf.variable_scope('encoder'): enc_outputs, enc_state = tf.nn.dynamic_rnn(self.enc_cell, src_emb, src_size, dtype=tf.float32) # 使用dynamic_rnn构造解码器 # 解码器读取目标句子每个位置的词向量,输出的dec_outputs为每一步顶层LSTM的输出 # dec_outputs的维度是[batch_size, max_time, HIDDEN_SIZE] # initial_state=enc_state表示用编码器的输出来初始化第一步的隐藏状态 # 编码器最后编码结束最后的状态为解码器初始化的状态 with tf.variable_scope('decoder'): dec_outputs, _ = tf.nn.dynamic_rnn(self.dec_cell, trg_emb, trg_size, initial_state=enc_state) # 计算解码器每一步的log perplexity # 输出重新转换成shape为[,HIDDEN_SIZE] output = tf.reshape(dec_outputs, [-1, HIDDEN_SIZE]) # 计算解码器每一步的softmax概率值 logits = tf.matmul(output, self.softmax_weight) + self.softmax_bias # 交叉熵损失函数,算loss loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.reshape(trg_label, [-1]), logits=logits) # 在计算平均损失时,需要将填充位置的权重设置为0,以避免无效位置的预测干扰模型的训练 label_weights = tf.sequence_mask(trg_size, maxlen=tf.shape(trg_label)[1], dtype=tf.float32) label_weights = tf.reshape(label_weights, [-1]) cost = tf.reduce_sum(loss * label_weights) cost_per_token = cost / tf.reduce_sum(label_weights) # 定义反向传播操作 trainable_variables = tf.trainable_variables() # 控制梯度大小,定义优化方法和训练步骤 # 算出每个需要更新的值的梯度,并对其进行控制 grads = tf.gradients(cost / tf.to_float(batch_size), trainable_variables) grads, _ = tf.clip_by_global_norm(grads, MAX_GRAD_NORM) # 利用梯度下降优化算法进行优化.学习率为1.0 optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) # 相当于minimize的第二步,正常来讲所得到的list[grads,vars]由compute_gradients得到,返回的是执行对应变量的更新梯度操作的op train_op = optimizer.apply_gradients(zip(grads, trainable_variables)) return cost_per_token, train_op """ function: 使用给定的模型model上训练一个epoch,并返回全局步数,每训练200步便保存一个checkpoint Parameters: session : 会议 cost_op : 计算loss的操作op train_op: 训练的操作op saver:  保存model的类 step:   训练步数 Returns: """ def run_epoch(session, cost_op, train_op, saver, step): # 训练一个epoch # 重复训练步骤直至遍历完Dataset中所有数据 while True: try: # 运行train_op并计算cost_op的结果也就是损失值,训练数据在main()函数中以Dataset方式提供 cost, _ = session.run([cost_op, train_op]) # 步数为10的倍数进行打印 if step % 10 == 0: print('After %d steps, per token cost is %.3f' % (step, cost)) # 每200步保存一个checkpoint if step % 200 == 0: saver.save(session, CHECKPOINT_PATH, global_step=step) step += 1 except tf.errors.OutOfRangeError: break return step """ function: 主函数 Parameters: Returns: """ def main(): # 定义初始化函数 initializer = tf.random_uniform_initializer(-0.05, 0.05) # 定义训练用的循环神经网络模型 with tf.variable_scope('nmt_model', reuse=None, initializer=initializer): train_model = NMTModel() # 定义输入数据 data = MakeSrcTrgDataset(SRC_TRAIN_DATA, TRG_TRAIN_DATA, BATCH_SIZE) iterator = data.make_initializable_iterator() (src, src_size), (trg_input, trg_label, trg_size) = iterator.get_next() # 定义前向计算图,输入数据以张量形式提供给forward函数 cost_op, train_op = train_model.forward(src, src_size, trg_input, trg_label, trg_size) # 训练模型 # 保存模型 saver = tf.train.Saver() step = 0 with tf.Session() as sess: # 初始化全部变量 tf.global_variables_initializer().run() # 进行NUM_EPOCH轮数 for i in range(NUM_EPOCH): print('In iteration: %d' % (i + 1)) sess.run(iterator.initializer) step = run_epoch(sess, cost_op, train_op, saver, step) if __name__ == '__main__': main() ``` 问题如下,不知道怎么解决,谢谢! Traceback (most recent call last): File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call return fn(*args) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn options, feed_dict, fetch_list, target_list, run_metadata) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.InvalidArgumentError: StringToNumberOp could not correctly convert string: This [[{{node StringToNumber}}]] [[{{node IteratorGetNext}}]] During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/Python37/untitled1/train_model.py", line 277, in <module> main() File "D:/Python37/untitled1/train_model.py", line 273, in main step = run_epoch(sess, cost_op, train_op, saver, step) File "D:/Python37/untitled1/train_model.py", line 231, in run_epoch cost, _ = session.run([cost_op, train_op]) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 929, in run run_metadata_ptr) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run feed_dict_tensor, options, run_metadata) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run run_metadata) File "D:\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: StringToNumberOp could not correctly convert string: This [[{{node StringToNumber}}]] [[node IteratorGetNext (defined at D:/Python37/untitled1/train_model.py:259) ]]

大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了

大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

程序员请照顾好自己,周末病魔差点一套带走我。

程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

上班一个月,后悔当初着急入职的选择了

最近有个老铁,告诉我说,上班一个月,后悔当初着急入职现在公司了。他之前在美图做手机研发,今年美图那边今年也有一波组织优化调整,他是其中一个,在协商离职后,当时捉急找工作上班,因为有房贷供着,不能没有收入来源。所以匆忙选了一家公司,实际上是一个大型外包公司,主要派遣给其他手机厂商做外包项目。**当时承诺待遇还不错,所以就立马入职去上班了。但是后面入职后,发现薪酬待遇这块并不是HR所说那样,那个HR自...

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

程序员写出这样的代码,能不挨骂吗?

当你换槽填坑时,面对一个新的环境。能够快速熟练,上手实现业务需求是关键。但是,哪些因素会影响你快速上手呢?是原有代码写的不够好?还是注释写的不够好?昨夜...

我说我不会算法,阿里把我挂了。

不说了,字节跳动也反手把我挂了。

带了6个月的徒弟当了面试官,而身为高级工程师的我天天修Bug......

即将毕业的应届毕业生一枚,现在只拿到了两家offer,但最近听到一些消息,其中一个offer,我这个组据说客户很少,很有可能整组被裁掉。 想问大家: 如果我刚入职这个组就被裁了怎么办呢? 大家都是什么时候知道自己要被裁了的? 面试软技能指导: BQ/Project/Resume 试听内容: 除了刷题,还有哪些技能是拿到offer不可或缺的要素 如何提升面试软实力:简历, 行为面试,沟通能...

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

C语言的灵魂之指针

指针在C语言中非常的重要,也比较的难。你对指针的掌握程度也决定了你对C语言的掌握程度。在学习C语言的时候因当搞清楚取值运算符和取地址运算符各自的含义。我们这里就通过取值运算符和取地址运算符展开讲指针。

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

10个提升效率的编程好习惯

文章目录00、前言01、双屏02、手机静音03、只学一个 00、前言 最近有读者反映学习编程困难,想放弃,询问是不是真的有不适合编程的人?我结合最近自己的学习经历和思考以及最近在数据结构与算法的折磨下得出的一些心得。 唐代大臣魏徵的【谏太宗十思疏】中写道,“善始者实繁,克终者盖寡”。大概的意思是:如果有好多人同时做一件事情,善于开始去做的人是非常多的,但是能够把这件事做好的人就寥寥无几了。 这句千...

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试:第十六章:Java中级开发

HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...

《Java经典编程365例》000:学妹的优秀成绩单

内容导航: 1、题目 2、代码实现 2.1、Student类 2.2、测试类Client 2.3、输出结果 前言 这是一个真实的故事,就发生在CSDN…其它学弟学妹开始抄吧! 1、题目 新建一个学生(Student),成员变量(属性)有: 姓名 性别 年龄(int) 成绩(double) 成员方法有两个: print()方法 功能为:输出名字 + 性别 + 年龄。

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

Python爬虫,高清美图我全都要(彼岸桌面壁纸)

爬取彼岸桌面网站较为简单,用到了requests、lxml、Beautiful Soup4

无代码时代来临,程序员如何保住饭碗?

编程语言层出不穷,从最初的机器语言到如今2500种以上的高级语言,程序员们大呼“学到头秃”。程序员一边面临编程语言不断推陈出新,一边面临由于许多代码已存在,程序员编写新应用程序时存在重复“搬砖”的现象。 无代码/低代码编程应运而生。无代码/低代码是一种创建应用的方法,它可以让开发者使用最少的编码知识来快速开发应用程序。开发者通过图形界面中,可视化建模来组装和配置应用程序。这样一来,开发者直...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

6年开发经验女程序员,面试京东Java岗要求薪资28K

写在开头: 上周面试了一位女程序员,上午10::30来我们部门面试,2B哥接待了她.来看看她的简历: 个人简历 个人技能: ● 熟悉spring mvc 、spring、mybatis 等框架 ● 熟悉 redis 、rocketmq、dubbo、zookeeper、netty 、nginx、tomcat、mysql。 ● 阅读过juc 中的线程池、锁的源...

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

Java岗开发3年,公司临时抽查算法,离职后这几题我记一辈子

前几天我们公司做了一件蠢事,非常非常愚蠢的事情。我原以为从学校出来之后,除了找工作有测试外,不会有任何与考试有关的事儿。 但是,天有不测风云,公司技术总监、人事总监两位大佬突然降临到我们事业线,叫上我老大,给我们组织了一场别开生面的“考试”。 那是一个风和日丽的下午,我翘着二郎腿,左手端着一杯卡布奇诺,右手抓着我的罗技鼠标,滚动着轮轴,穿梭在头条热点之间。 “淡黄的长裙~蓬松的头发...

都前后端分离了,咱就别做页面跳转了!统统 JSON 交互

文章目录1. 无状态登录1.1 什么是有状态1.2 什么是无状态1.3 如何实现无状态1.4 各自优缺点2. 登录交互2.1 前后端分离的数据交互2.2 登录成功2.3 登录失败3. 未认证处理方案4. 注销登录 这是本系列的第四篇,有小伙伴找不到之前文章,松哥给大家列一个索引出来: 挖一个大坑,Spring Security 开搞! 松哥手把手带你入门 Spring Security,别再问密...

面试官:你连SSO都不懂,就别来面试了

大厂竟然要考我SSO,卧槽。

程序员是做全栈工程师好?还是专注一个领域好?

昨天,有位大一的同学私信我,说他要做全栈工程师。 我一听,这不害了孩子么,必须制止啊。 谁知,讲到最后,更确定了他做全栈程序员的梦想。 但凡做全栈工程师的,要么很惨,要么很牛! 但凡很牛的,绝不是一开始就是做全栈的! 全栈工程师听起来好听,但绝没有你想象的那么简单。 今天听我来给你唠,记得帮我点赞哦。 一、全栈工程师的职责 如果你学习编程的目的只是玩玩,那随意,想怎么学怎么学。...

立即提问
相关内容推荐