python placeholder函数的问题

import tensorflow as tf
b=tf.Variable(tf.zeros([100]))
w=tf.Variable(tf.random_uniform([784,100],-1,1))
x=tf.placeholder(name="x")

出现如下错误:
TypeError: placeholder() takes at least 1 argument (1 given)

请教如何解决

2个回答

placeholder 有个必须的参数 dtype 设置上 试试

换成这个试试:
x=tf.placeholder(dtype=tf.float, name="x")

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
python测试集结果调取问题
test_acc = sess.run(accr,feed_dict=feeds_test) 这个语句是用来调出测试准确率的,如何才能调出测试期间对每一个样本的预测数值???? x=tf.placeholder("float", [None,784]) #placeholder 占位,不赋给x实际值,784 像素值, None无穷样本 y=tf.placeholder("float", [None,10]) #10个分类目标 W=tf.Variable(tf.zeros([784,10])) b=tf.Variable(tf.zeros([10])) #tf.zeros 初始化 actv= tf.nn.softmax(tf.matmul(x,W)+b) #cost function cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1)) learning_rate=0.01 optm= tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) print ('1') pred=tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1)) #ACCURACY accr=tf.reduce_mean(tf.cast(pred,"float")) #INITIALIZER init=tf.global_variables_initializer() training_epochs = 50 #所有样本迭代次数=50 batch_size = 100 #每次迭代用多少样本 display_step = 5 #展示 sess=tf.Session() sess.run(init) #跑初始化 for epoch in range (training_epochs): avg_cost=0 num_batch=int(mnist.train.num_examples/batch_size) for i in range (num_batch): batch_xs, batch_ys= mnist.train.next_batch(batch_size) #一步一步的往下找 sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) feeds={x:batch_xs, y: batch_ys} avg_cost += sess.run (cost, feed_dict=feeds)/num_batch #display if epoch % display_step == 0: feeds_train = {x: batch_xs, y: batch_ys} feeds_test = {x: mnist.test.images, y: mnist.test.labels} train_acc = sess.run(accr, feed_dict=feeds_train) #feed_dict 针对place holder占位 test_acc = sess.run(accr,feed_dict=feeds_test) print ("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f" % (epoch, training_epochs, avg_cost, train_acc, test_acc))
用tensorflow写一个简单的神经网络识别mnist出现问题(python)
``` #放入每个批次的数量 batch_size = 200 #计算有多少批次 n_batch = mnist.train.num_examples // batch_size #定义两个占位符 x = tf.placeholder(tf.float32,[None,784]) y = tf.placeholder(tf.float32,[None,10]) #创建神经网络中间层 W1 = tf.Variable(tf.zeros([784,100])) b1 = tf.Variable(tf.zeros([784,100])) L1 = tf.nn.sigmoid(tf.matmul(x,W1) + b1) #创建神经网络输出层 W = tf.Variable(tf.zeros([100,10])) b = tf.Variable(tf.zeros([10])) prediction = tf.nn.softmax(tf.matmul(L1,W) + b) #定义二次代价函数 loss = tf.reduce_mean(tf.square(y-prediction)) #梯度下降 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) init = tf.global_variables_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.Session() as sess: sess.run(init) for epoch in range(21): for batch in range(n_batch): batch_xs,batch_ys = mnist.train.next_batch(batch_size) sess.run(train_step,feed_dict = {x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}) print("iter " + str(epoch) + "testing accuracy " + str(acc)) ``` 本来没有隐层,想加一个隐层,然后哦出现各种问题,然后有一个报错不知道怎么解决,求教,谢谢! InvalidArgumentError Traceback (most recent call last) F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 1326 try: -> 1327 return fn(*args) 1328 except errors.OpError as e: F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 1305 feed_dict, fetch_list, target_list, -> 1306 status, run_metadata) 1307 F:\Program Files\Anaconda\lib\contextlib.py in __exit__(self, type, value, traceback) 65 try: ---> 66 next(self.gen) 67 except StopIteration: F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\framework\errors_impl.py in raise_exception_on_not_ok_status() 465 compat.as_text(pywrap_tensorflow.TF_Message(status)), --> 466 pywrap_tensorflow.TF_GetCode(status)) 467 finally: InvalidArgumentError: Incompatible shapes: [200,10] vs. [784,10] [[Node: gradients_2/sub_2_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](gradients_2/sub_2_grad/Shape, gradients_2/sub_2_grad/Shape_1)]] During handling of the above exception, another exception occurred: InvalidArgumentError Traceback (most recent call last) <ipython-input-6-c3ad0ddd192e> in <module>() 35 for batch in range(n_batch): 36 batch_xs,batch_ys = mnist.train.next_batch(batch_size) ---> 37 sess.run(train_step,feed_dict = {x:batch_xs,y:batch_ys}) 38 39 acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}) F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata) 893 try: 894 result = self._run(None, fetches, feed_dict, options_ptr, --> 895 run_metadata_ptr) 896 if run_metadata: 897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1122 if final_fetches or final_targets or (handle and feed_dict_tensor): 1123 results = self._do_run(handle, final_targets, final_fetches, -> 1124 feed_dict_tensor, options, run_metadata) 1125 else: 1126 results = [] F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1319 if handle is None: 1320 return self._do_call(_run_fn, self._session, feeds, fetches, targets, -> 1321 options, run_metadata) 1322 else: 1323 return self._do_call(_prun_fn, self._session, handle, feeds, fetches) F:\Program Files\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 1338 except KeyError: 1339 pass -> 1340 raise type(e)(node_def, op, message) 1341 1342 def _extend_graph(self): InvalidArgumentError: Incompatible shapes: [200,10] vs. [784,10]
tensorflow,python运行时报错在reshape上,求大神解答
代码来自于一篇博客,用tensorflow判断拨号图标和短信图标的分类,训练已经成功运行,以下为测试代码,错误出现在38行 image = tf.reshape(image, [1, 208, 208, 3]) 我的测试图片是256*256的,也测试了48*48的 ``` #!/usr/bin/python # -*- coding:utf-8 -*- # @Time : 2018/3/31 0031 17:50 # @Author : scw # @File : main.py # 进行图片预测方法调用的文件 import numpy as np from PIL import Image import tensorflow as tf import matplotlib.pyplot as plt from shenjingwangluomodel import inference # 定义需要进行分类的种类,这里我是进行分两种,因为一种为是拨号键,另一种就是非拨号键 CallPhoneStyle = 2 # 进行测试的操作处理========================== # 加载要进行测试的图片 def get_one_image(img_dir): image = Image.open(img_dir) # Image.open() # 好像一次只能打开一张图片,不能一次打开一个文件夹,这里大家可以去搜索一下 plt.imshow(image) image = image.resize([208, 208]) image_arr = np.array(image) return image_arr # 进行测试处理------------------------------------------------------- def test(test_file): # 设置加载训练结果的文件目录(这个是需要之前就已经训练好的,别忘记) log_dir = '/home/administrator/test_system/calldata2/' # 打开要进行测试的图片 image_arr = get_one_image(test_file) with tf.Graph().as_default(): # 把要进行测试的图片转为tensorflow所支持的格式 image = tf.cast(image_arr, tf.float32) # 将图片进行格式化的处理 image = tf.image.per_image_standardization(image) # 将tensorflow的图片的格式参数,转变为shape格式的,好像就是去掉-1这样的列表 image = tf.reshape(image, [1, 208, 208, 3]) # print(image.shape) # 参数CallPhoneStyle:表示的是分为两类 p = inference(image, 1, CallPhoneStyle) # 这是训练出一个神经网络的模型 # 这里用到了softmax这个逻辑回归模型的处理 logits = tf.nn.softmax(p) x = tf.placeholder(tf.float32, shape=[208, 208, 3]) saver = tf.train.Saver() with tf.Session() as sess: # 对tensorflow的训练参数进行初始化,使用默认的方式 sess.run(tf.global_variables_initializer()) # 判断是否有进行训练模型的设置,所以一定要之前就进行了模型的训练 ckpt = tf.train.get_checkpoint_state(log_dir) if ckpt and ckpt.model_checkpoint_path: # global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) # 调用saver.restore()函数,加载训练好的网络模型 print('Loading success') else: print('No checkpoint') prediction = sess.run(logits, feed_dict={x: image_arr}) max_index = np.argmax(prediction) print('预测的标签为:') if max_index == 0: print("是拨号键图片") else: print("是短信图片") # print(max_index) print('预测的分类结果每种的概率为:') print(prediction) # 我用0,1表示两种分类,这也是我在训练的时候就设置好的 if max_index == 0: print('图片是拨号键图标的概率为 %.6f' %prediction[:, 0]) elif max_index == 1: print('图片是短信它图标的概率为 %.6f' %prediction[:, 1]) # 进行图片预测 test('/home/administrator/Downloads/def.png') ''' # 测试自己的训练集的图片是不是已经加载成功(因为这个是进行训练的第一步) train_dir = 'E:/tensorflowdata/calldata/' BATCH_SIZE = 5 # 生成批次队列中的容量缓存的大小 CAPACITY = 256 # 设置我自己要对图片进行统一大小的高和宽 IMG_W = 208 IMG_H = 208 image_list,label_list = get_files(train_dir) # 加载训练集的图片和对应的标签 image_batch,label_batch = get_batch(image_list,label_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY) # 进行批次图片加载到内存中 # 这是打开一个session,主要是用于进行图片的显示效果的测试 with tf.Session() as sess: i = 0 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) try: while not coord.should_stop() and i < 2: # 提取出两个batch的图片并可视化。 img, label = sess.run([image_batch, label_batch]) for j in np.arange(BATCH_SIZE): print('label: %d' % label[j]) plt.imshow(img[j, :, :, :]) plt.show() i += 1 except tf.errors.OutOfRangeError: print('done!') finally: coord.request_stop() coord.join(threads) ''' ```
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 ```
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实现BP算法遇到了问题,求大神指点!!!
import tensorflow as tf import numpy as np #from tensorflow.examples.tutorials.mnist import input_data #载入数据集 #mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次的大小 #batch_size = 100 #????????????????????????????????? #计算一共有多少个批次 #n_batch = mnist.train.num_examples // batch_size #定义placeholder x_data=np.mat([[0.4984,0.5102,0.5213,0.5340], [0.5102,0.5213,0.5340,0.5407], [0.5213,0.5340,0.5407,0.5428], [0.5340,0.5407,0.5428,0.5530], [0.5407,0.5428,0.5530,0.5632], [0.5428,0.5530,0.5632,0.5739], [0.5530,0.5632,0.5739,0.5821], [0.5632,0.5739,0.5821,0.5920], [0.5739,0.5821,0.5920,0.5987], [0.5821,0.5920,0.5987,0.6043], [0.5920,0.5987,0.6043,0.6095], [0.5987,0.6043,0.6095,0.6161], [0.6043,0.6095,0.6161,0.6251], [0.6095,0.6161,0.6251,0.6318], [0.6161,0.6251,0.6318,0.6387], [0.6251,0.6318,0.6387,0.6462], [0.6318,0.6387,0.6462,0.6518], [0.6387,0.6462,0.6518,0.6589], [0.6462,0.6518,0.6589,0.6674], [0.6518,0.6589,0.6674,0.6786], [0.6589,0.6674,0.6786,0.6892], [0.6674,0.6786,0.6892,0.6988]]) y_data=np.mat([[0.5407], [0.5428], [0.5530], [0.5632], [0.5739], [0.5821], [0.5920], [0.5987], [0.6043], [0.6095], [0.6161], [0.6251], [0.6318], [0.6387], [0.6462], [0.6518], [0.6589], [0.6674], [0.6786], [0.6892], [0.6988], [0.7072]]) xs = tf.placeholder(tf.float32,[None,4]) # 样本数未知,特征数为1,占位符最后要以字典形式在运行中填入 ys = tf.placeholder(tf.float32,[None,1]) #创建一个简单的神经网络 W1 = tf.Variable(tf.truncated_normal([4,10],stddev=0.1)) b1 = tf.Variable(tf.zeros([10])+0.1) L1 = tf.nn.tanh(tf.matmul(x,W1)+b1) W2 = tf.Variable(tf.truncated_normal([10,1],stddev=0.1)) b2 = tf.Variable(tf.zeros([1])+0.1) L2 = tf.nn.softmax(tf.matmul(L1,W2)+b2) #二次代价函数 #loss = tf.reduce_mean(tf.square(y-prediction)) #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys,logits=prediction)) loss = tf.reduce_mean(tf.reduce_sum(tf.square((ys-L2)),reduction_indices = [1]))#需要向相加索引号,redeuc执行跨纬度操作 #使用梯度下降法 #train_step = tf.train.GradientDescentOptimizer(0.1).mnimize(loss) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) #train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 选择梯度下降法 #初始化变量 #init = tf.global_variables_initializer() init = tf.initialize_all_variables() #结果存放在一个布尔型列表中 #correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #求准确率 #accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess: sess.run(init) for epoch in range(21): for i in range(22): #batch_xs,batch_ys = mnist.train.next_batch(batch_size) #????????????????????????? sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) #test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0}) #train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0}) print (sess.run(prediction,feed_dict={xs:x_data,ys:y_data})) 提示:WARNING:tensorflow:From <ipython-input-10-578836c021a3>:89 in <module>.: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. --------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) C:\Users\Administrator\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args) 1020 try: -> 1021 return fn(*args) 1022 except errors.OpError as e: C:\Users\Administrator\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 1002 feed_dict, fetch_list, target_list, -> 1003 status, run_metadata) 1004 。。。
tensorflow 在feed的时候报错could not convert string to float
报错ValueError: could not convert string to float: 'C:/Users/Administrator/Desktop/se//12.21/FV-USM/Published_database_FV-USM_Dec2013/1st_session/extractedvein/vein001_1/01.jpg' 以下是我的代码 ```#!/usr/bin/env python # -*- coding:utf-8 -*- import tensorflow as tf from PIL import Image import model import numpy as np import os def get_one_image(): i=1 n=1 k=1 i = str(i) n = str(n) k = str(k) a = i.zfill(3) b = n.zfill(1) c = k.zfill(2) files='C:/Users/Administrator/Desktop/se//12.21/FV-USM/Published_database_FV-USM_Dec2013/1st_session/extractedvein/vein' + a + '_' + b + '/' + c + '.jpg' # image=image.resize([60,175]) # image=np.array(image) return files def evaluate_one_image(): image_array = get_one_image() with tf.Graph().as_default(): BATCH_SIZE=1 N_CLASS=123 image = tf.image.decode_jpeg(image_array, channels=1) image = tf.image.resize_image_with_crop_or_pad(image,60, 175) image = tf.image.per_image_standardization(image) ###图片调整完成 # image=tf.cast(image,tf.string) # image1 = tf.image.decode_jpeg(image, channels=1) # image1 = tf.image.resize_image_with_crop_or_pad(image1, image_w, image_h) #image1 = tf.image.per_image_standardization(image) ###图片调整完成 #image=tf.image.per_image_standardization(image) image = tf.cast(image, tf.float32) image=tf.reshape(image,[1,60,175,1]) logit = model.inference(image, BATCH_SIZE, N_CLASS) logit=tf.nn.softmax(logit) # 因为 inference 的返回没有用激活函数,所以在这里对结果用softmax 激活。但是为什么要激活?? x=tf.placeholder(tf.float32,shape=[60,175,1]) logs_model_dir='C:/Users/Administrator/Desktop/python的神经网络/model/' saver=tf.train.Saver() with tf.Session() as sess: print("从指定路径加载模型。。。") ckpt=tf.train.get_checkpoint_state(logs_model_dir) if ckpt and ckpt.model_checkpoint_path: global_step=ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess,ckpt.model_checkpoint_path) print('模型加载成功,训练步数为 %s'%global_step) else: print('模型加载失败,,,,文件没有找到') ###IMG = sess.run(image) prediction=sess.run(logit,feed_dict={x:image_array}) max_index=np.argmax(prediction) print(max_index) evaluate_one_image() ```
tensorflow实现多层感知机,出现内存泄漏!
``` # coding: UTF-8 # TensorFlow实现Softmax Regression识别手写数字(多层感知机) import tensorflow as tf ########加载数据集######## from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) sess = tf.InteractiveSession() in_units = 784 h1_units = 300 w1 = tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1)) b1 = tf.Variable(tf.zeros([h1_units])) w2 = tf.Variable(tf.zeros([h1_units,10])) b2 = tf.Variable(tf.zeros([10])) x = tf.placeholder(tf.float32,[None,in_units]) keep_prob = tf.placeholder(tf.float32) ########定义模型结构######## hidden1 = tf.nn.relu(tf.matmul(x,w1)+b1) hidden1_drop = tf.nn.dropout(hidden1,keep_prob) y = tf.nn.softmax(tf.matmul(hidden1_drop,w2) + b2) ########定义损失函数和选择优化器来优化loss######## y_ = tf.placeholder(tf.float32,[None,10]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices = [1])) train_step = tf.train.AdagradOptimizer(0,3).minimize(cross_entropy) tf.global_variables_initializer().run() for i in range(3000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x: batch_xs, y_: batch_ys,keep_prob:0.75}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})) ```
softmax分类器,分类概率都是nan是怎么回事?
训练结果有测试集和训练集的准确率,但是调取测试集的预测值时,全都是nan,我需要得到测试结果实际应用的。请问怎么解决?拿一个二分类为例 以下是前期搭建框架 import numpy as np import tensorflow as tf x=tf.placeholder("float", [None,115]) y=tf.placeholder("float", [None,2]) W=tf.Variable(tf.zeros([115,2])) b=tf.Variable(tf.zeros([2])) actv= tf.nn.softmax(tf.matmul(x,W)+b) ####网上查到说下面这个loss函数中如果是log(0)的话就会有nan,但那应该连训练结果都没有才对吧?我现在能得到训练结果,但是结果都是nan怎么办? cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1)) learning_rate=0.01 optm= tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) pred=tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1)) accr=tf.reduce_mean(tf.cast(pred,"float")) init=tf.global_variables_initializer() sess=tf.InteractiveSession() sess=tf.Session() sess.run(init) training_lenth=len(G)####(回测长度) training_epochs =50 #训练次数 batch_size = len(G) #每次迭代用多少样本(用全套) ##display_step = 5 #展示 print('Down') 训练重点来了,我需要得到result的实际结果 lenth=2 for epoch in range(training_epochs): avg_cost=0 num_batch=int(len(G)/batch_size) for i in range((lenth-1),lenth): batch_xs=np.array(G[i]) batch_ys=np.array(F[i]) sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) feeds={x:batch_xs, y: batch_ys} avg_cost += sess.run (cost, feed_dict=feeds)/(num_batch*lenth) feeds_train = {x: batch_xs, y: batch_ys} feeds_test = {x: G[i+1], y: F[i+1]} train_acc = sess.run(accr, feed_dict=feeds_train) #feed_dict 针对place holder占位 test_acc = sess.run(accr,feed_dict=feeds_test) result=sess.run(actv,feed_dict=feeds_test) 但是实际给我的结果中,test_acc和train_acc都是有的,但是具体分类的概率值都是nan。 result=sess.run(actv,feed_dict=feeds_train) print (train_acc)# print(test_acc) train_acc a=sess.run([accr,actv],feed_dict=feeds_test) print(a) 0.930233 0.465116 [0.46511629, array([[ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan], [ nan, nan]], dtype=float32)] 求大神指教,跪送分
各位大神好,我在用tensorflow跑多任务学习的多层感知机遇到问题,训练得到y值都基本一样
# -*- coding: utf-8 -*- """ Created on Fri Mar 30 18:09:10 2018 @author: 32641 """ import tensorflow as tf import xlrd import numpy as np data2=[] data0=[] data = xlrd.open_workbook('C:/Users/32641/Desktop/论文2/汇率上证.xls') table = data.sheet_by_name('Sheet1') nrows = table.nrows for rownum in range(1, nrows): #也就是从Excel第二行开始,第一行表头不算 row = table.row_values(rownum) #for i in range(1,ncols):heet #row[i]=(row[i]-meansl[i-1])/stdl[i-1] #row[i]=(row[i]-minsl[i-1])/(maxsl[i-1]-minsl[i-1]) #date = xlrd.xldate_as_tuple(row[0],0) #date1=str(date[0])+'.'+str(date[1])+'.'+str(date[2]) #row[0]=date1 data0.append(row[1]) data2.append(row[2]) timeseries1=[] timeseries2=[] timeseriess=data0 for q in range(len(timeseriess)): timeseries1.append((timeseriess[q]-min(timeseriess))/(max(timeseriess)-min(timeseriess))) timeseries1=np.array(timeseries1) for q in range(len(timeseriess)): timeseries2.append((timeseriess[q]-min(timeseriess))/(max(timeseriess)-min(timeseriess))) timeseries2=np.array(timeseries2) lag=1 def cut(timeseries,lag): x=np.zeros((1,lag)) y=np.zeros((1,1)) for i in range(len(timeseries)-lag): x=np.vstack((x,timeseries[i:i+lag])) y=np.vstack((y,timeseries[i+lag])) return x[1:],y[1:] x1,y1=cut(timeseries1,lag) x2,y2=cut(timeseries2,lag) split_boundary = int(x1.shape[0] * 0.8) train_x1=x1[:split_boundary] test_x1=x1[split_boundary:] train_y1=x1[:split_boundary] test_y1=x1[split_boundary:] train_x2=x2[:split_boundary] test_x2=x2[split_boundary:] train_y2=x2[:split_boundary] test_y2=x2[split_boundary:] # 定义占位符 X = tf.placeholder("float",name="X") Y1 = tf.placeholder("float", name="Y1") Y2 = tf.placeholder("float", name="Y2") # 定义权重 initial_shared_layer_weights = np.random.rand(1,2) initial_Y1_layer_weights = np.random.rand(2,1) initial_Y2_layer_weights = np.random.rand(2,1) shared_layer_weights = tf.Variable(initial_shared_layer_weights, name="share_W", dtype="float32") Y1_layer_weights = tf.Variable(initial_Y1_layer_weights, name="share_Y1", dtype="float32") Y2_layer_weights = tf.Variable(initial_Y2_layer_weights, name="share_Y2", dtype="float32") # 使用relu激活函数构建层 shared_layer = tf.nn.sigmoid(tf.matmul(X,shared_layer_weights)) Y1_layer = tf.nn.sigmoid(tf.matmul(shared_layer,Y1_layer_weights)) Y2_layer = tf.nn.sigmoid(tf.matmul(shared_layer,Y2_layer_weights)) # 计算loss Y1_Loss = tf.nn.l2_loss(Y1-Y1_layer) Y2_Loss = tf.nn.l2_loss(Y2-Y2_layer) # 优化器 Y1_op = tf.train.AdamOptimizer().minimize(Y1_Loss) Y2_op = tf.train.AdamOptimizer().minimize(Y2_Loss) # Calculation (Session) Code # ========================== # open the session with tf.Session() as session: session.run(tf.initialize_all_variables()) for iters in range(3000): if np.random.rand() < 0.5: _, Y1_loss,Y1_Layer= session.run([Y1_op, Y1_Loss,Y1_layer], { X: train_x1, Y1: train_y1, Y2: train_y2 }) print(Y1_Layer) print(Y1_loss) else: _, Y2_loss= session.run([Y2_op, Y2_Loss], { X: train_x2, Y1: train_y1, Y2: train_y2 }) print(Y2_loss) session.close()
tensorflow debug 调试错误
# -*- coding: utf-8 -*- """ Created on Sat Oct 28 15:40:51 2017 @author: Administrator """ import tensorflow as tf from tensorflow.python import debug as tf_debug '''载入数据''' from aamodule import input_data mnist = input_data.read_data_sets('d://MNIST',one_hot=True) '''构建回归模型''' #定义回归模型 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])) y_ = tf.matmul(x,W) + b #预测值 #定义损失函数和优化器 cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) '''训练模型''' sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) sess = tf_debug.LocalCLIDebugWrapperSession(sess,ui_type='readline') #sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan) #Train for i in range(1000): batch_xs,batch_ys = mnist.train.next_batch(100) sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) #评估训练好的模型 correct_prediction = tf.equal(tf.argmax(y_,1),tf.argmax(y,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #计算模型在测试集上的准确率 print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})) 加入sess = tf_debug.LocalCLIDebugWrapperSession(sess,ui_type='readline')后就运行不了了,ValueError: Exhausted all fallback ui_types. ``` ```
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()
我的mnist运行报错,请问是那出现问题了?
from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse #解析训练和检测数据模块 import sys from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf FLAGS = None def main(_): # Import data mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) # Create the model x = tf.placeholder(tf.float32, [None, 784]) #此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值 W = tf.Variable(tf.zeros([784, 10])) # tf.zeros表示所有的维度都为0 b = tf.Variable(tf.zeros([10])) y = tf.matmul(x, W) + b #对应每个分类概率值。 # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 10]) # The raw formulation of cross-entropy, # # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), # reduction_indices=[1])) # # can be numerically unstable. # # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw # outputs of 'y', and then average across the batch. cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.InteractiveSession() tf.global_variables_initializer().run() # Train for _ in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # Test trained model correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data', help='Directory for storing input data') FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) ``` ```下面是报错: TimeoutError Traceback (most recent call last) ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args) 1317 h.request(req.get_method(), req.selector, req.data, headers, -> 1318 encode_chunked=req.has_header('Transfer-encoding')) 1319 except OSError as err: # timeout error ~\Anaconda3\envs\tensorflow\lib\http\client.py in request(self, method, url, body, headers, encode_chunked) 1238 """Send a complete request to the server.""" -> 1239 self._send_request(method, url, body, headers, encode_chunked) 1240 ~\Anaconda3\envs\tensorflow\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked) 1284 body = _encode(body, 'body') -> 1285 self.endheaders(body, encode_chunked=encode_chunked) 1286 ~\Anaconda3\envs\tensorflow\lib\http\client.py in endheaders(self, message_body, encode_chunked) 1233 raise CannotSendHeader() -> 1234 self._send_output(message_body, encode_chunked=encode_chunked) 1235 ~\Anaconda3\envs\tensorflow\lib\http\client.py in _send_output(self, message_body, encode_chunked) 1025 del self._buffer[:] -> 1026 self.send(msg) 1027 ~\Anaconda3\envs\tensorflow\lib\http\client.py in send(self, data) 963 if self.auto_open: --> 964 self.connect() 965 else: ~\Anaconda3\envs\tensorflow\lib\http\client.py in connect(self) 1399 self.sock = self._context.wrap_socket(self.sock, -> 1400 server_hostname=server_hostname) 1401 if not self._context.check_hostname and self._check_hostname: ~\Anaconda3\envs\tensorflow\lib\ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session) 400 server_hostname=server_hostname, --> 401 _context=self, _session=session) 402 ~\Anaconda3\envs\tensorflow\lib\ssl.py in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context, _session) 807 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") --> 808 self.do_handshake() 809 ~\Anaconda3\envs\tensorflow\lib\ssl.py in do_handshake(self, block) 1060 self.settimeout(None) -> 1061 self._sslobj.do_handshake() 1062 finally: ~\Anaconda3\envs\tensorflow\lib\ssl.py in do_handshake(self) 682 """Start the SSL/TLS handshake.""" --> 683 self._sslobj.do_handshake() 684 if self.context.check_hostname: TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 During handling of the above exception, another exception occurred: URLError Traceback (most recent call last) <ipython-input-1-eaf9732201f9> in <module>() 57 help='Directory for storing input data') 58 FLAGS, unparsed = parser.parse_known_args() ---> 59 tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) ~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py in run(main, argv) 46 # Call the main function, passing through any arguments 47 # to the final program. ---> 48 _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 49 50 <ipython-input-1-eaf9732201f9> in main(_) 15 def main(_): 16 # Import data ---> 17 mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) 18 19 # Create the model ~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py in read_data_sets(train_dir, fake_data, one_hot, dtype, reshape, validation_size, seed) 238 239 local_file = base.maybe_download(TRAIN_LABELS, train_dir, --> 240 SOURCE_URL + TRAIN_LABELS) 241 with open(local_file, 'rb') as f: 242 train_labels = extract_labels(f, one_hot=one_hot) ~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in maybe_download(filename, work_directory, source_url) 206 filepath = os.path.join(work_directory, filename) 207 if not gfile.Exists(filepath): --> 208 temp_file_name, _ = urlretrieve_with_retry(source_url) 209 gfile.Copy(temp_file_name, filepath) 210 with gfile.GFile(filepath) as f: ~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in wrapped_fn(*args, **kwargs) 163 for delay in delays(): 164 try: --> 165 return fn(*args, **kwargs) 166 except Exception as e: # pylint: disable=broad-except) 167 if is_retriable is None: ~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py in urlretrieve_with_retry(url, filename) 188 @retry(initial_delay=1.0, max_delay=16.0, is_retriable=_is_retriable) 189 def urlretrieve_with_retry(url, filename=None): --> 190 return urllib.request.urlretrieve(url, filename) 191 192 ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in urlretrieve(url, filename, reporthook, data) 246 url_type, path = splittype(url) 247 --> 248 with contextlib.closing(urlopen(url, data)) as fp: 249 headers = fp.info() 250 ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 221 else: 222 opener = _opener --> 223 return opener.open(url, data, timeout) 224 225 def install_opener(opener): ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in open(self, fullurl, data, timeout) 524 req = meth(req) 525 --> 526 response = self._open(req, data) 527 528 # post-process response ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in _open(self, req, data) 542 protocol = req.type 543 result = self._call_chain(self.handle_open, protocol, protocol + --> 544 '_open', req) 545 if result: 546 return result ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args) 502 for handler in handlers: 503 func = getattr(handler, meth_name) --> 504 result = func(*args) 505 if result is not None: 506 return result ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in https_open(self, req) 1359 def https_open(self, req): 1360 return self.do_open(http.client.HTTPSConnection, req, -> 1361 context=self._context, check_hostname=self._check_hostname) 1362 1363 https_request = AbstractHTTPHandler.do_request_ ~\Anaconda3\envs\tensorflow\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args) 1318 encode_chunked=req.has_header('Transfer-encoding')) 1319 except OSError as err: # timeout error -> 1320 raise URLError(err) 1321 r = h.getresponse() 1322 except: URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。> In [ ]:
Tensorflow用自己的图片做数据集做识别,无法feed数据,跪求大神帮助!
使用tensorflow识别我自己的tfrecord文件时,在训练时无法feed数据,错误是placeholder那里,下面给出错误和我的代码,跪求大神帮助!!! 错误: ``` Traceback (most recent call last): File "/Users/hanjiarong/PycharmProjects/sample5/main.py", line 206, in <module> session.run(opti, feed_dict={x: session.run(batch_image), y: session.run(batch_label), keep_drop: dropout}) File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 905, in run run_metadata_ptr) File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1113, in _run str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (1, 227, 227, 3) for Tensor 'Placeholder:0', which has shape '(154587, ?)' ``` 下面是我的代码: ``` import tensorflow as tf from encode_to_tfrecords import create_record, create_test_record, read_and_decode, get_batch, get_test_batch n_input = 154587 n_classes = 3 dropout = 0.5 x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.int32, [None, n_classes]) keep_drop = tf.placeholder(tf.float32) class network(object): def inference(self, images,keep_drop): #################################################################################################################### # 向量转为矩阵 # images = tf.reshape(images, shape=[-1, 39,39, 3]) images = tf.reshape(images, shape=[-1, 227, 227, 3]) # [batch, in_height, in_width, in_channels] images = (tf.cast(images, tf.float32) / 255. - 0.5) * 2 # 归一化处理 #################################################################################################################### # 第一层 定义卷积偏置和下采样 conv1 = tf.nn.bias_add(tf.nn.conv2d(images, self.weights['conv1'], strides=[1, 4, 4, 1], padding='VALID'), self.biases['conv1']) relu1 = tf.nn.relu(conv1) pool1 = tf.nn.max_pool(relu1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # 第二层 conv2 = tf.nn.bias_add(tf.nn.conv2d(pool1, self.weights['conv2'], strides=[1, 1, 1, 1], padding='SAME'), self.biases['conv2']) relu2 = tf.nn.relu(conv2) pool2 = tf.nn.max_pool(relu2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # 第三层 conv3 = tf.nn.bias_add(tf.nn.conv2d(pool2, self.weights['conv3'], strides=[1, 1, 1, 1], padding='SAME'), self.biases['conv3']) relu3 = tf.nn.relu(conv3) # pool3=tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') conv4 = tf.nn.bias_add(tf.nn.conv2d(relu3, self.weights['conv4'], strides=[1, 1, 1, 1], padding='SAME'), self.biases['conv4']) relu4 = tf.nn.relu(conv4) conv5 = tf.nn.bias_add(tf.nn.conv2d(relu4, self.weights['conv5'], strides=[1, 1, 1, 1], padding='SAME'), self.biases['conv5']) relu5 = tf.nn.relu(conv5) pool5 = tf.nn.max_pool(relu5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID') # 全连接层1,先把特征图转为向量 flatten = tf.reshape(pool5, [-1, self.weights['fc1'].get_shape().as_list()[0]]) # dropout比率选用0.5 drop1 = tf.nn.dropout(flatten, keep_drop) fc1 = tf.matmul(drop1, self.weights['fc1']) + self.biases['fc1'] fc_relu1 = tf.nn.relu(fc1) fc2 = tf.matmul(fc_relu1, self.weights['fc2']) + self.biases['fc2'] fc_relu2 = tf.nn.relu(fc2) fc3 = tf.matmul(fc_relu2, self.weights['fc3']) + self.biases['fc3'] return fc3 def __init__(self): # 初始化权值和偏置 with tf.variable_scope("weights"): self.weights = { # 39*39*3->36*36*20->18*18*20 'conv1': tf.get_variable('conv1', [11, 11, 3, 96], initializer=tf.contrib.layers.xavier_initializer_conv2d()), # 18*18*20->16*16*40->8*8*40 'conv2': tf.get_variable('conv2', [5, 5, 96, 256], initializer=tf.contrib.layers.xavier_initializer_conv2d()), # 8*8*40->6*6*60->3*3*60 'conv3': tf.get_variable('conv3', [3, 3, 256, 384], initializer=tf.contrib.layers.xavier_initializer_conv2d()), # 3*3*60->120 'conv4': tf.get_variable('conv4', [3, 3, 384, 384], initializer=tf.contrib.layers.xavier_initializer_conv2d()), 'conv5': tf.get_variable('conv5', [3, 3, 384, 256], initializer=tf.contrib.layers.xavier_initializer_conv2d()), 'fc1': tf.get_variable('fc1', [6 * 6 * 256, 4096], initializer=tf.contrib.layers.xavier_initializer()), 'fc2': tf.get_variable('fc2', [4096, 4096], initializer=tf.contrib.layers.xavier_initializer()), 'fc3': tf.get_variable('fc3', [4096, 1000], initializer=tf.contrib.layers.xavier_initializer()), } with tf.variable_scope("biases"): self.biases = { 'conv1': tf.get_variable('conv1', [96, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'conv2': tf.get_variable('conv2', [256, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'conv3': tf.get_variable('conv3', [384, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'conv4': tf.get_variable('conv4', [384, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'conv5': tf.get_variable('conv5', [256, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'fc1': tf.get_variable('fc1', [4096, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'fc2': tf.get_variable('fc2', [4096, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)), 'fc3': tf.get_variable('fc3', [1000, ], initializer=tf.constant_initializer(value=0.1, dtype=tf.float32)) } # 计算softmax交叉熵损失函数 def sorfmax_loss(self, predicts, labels): predicts = tf.nn.softmax(predicts) labels = tf.one_hot(labels, self.weights['fc3'].get_shape().as_list()[1]) loss = tf.nn.softmax_cross_entropy_with_logits(logits=predicts, labels=labels) # loss =-tf.reduce_mean(labels * tf.log(predicts))# tf.nn.softmax_cross_entropy_with_logits(predicts, labels) self.cost = loss return self.cost # 梯度下降 def optimer(self, loss, lr=0.01): train_optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss) return train_optimizer #定义训练 # def train(self): create_record('/Users/hanjiarong/Documents/testdata/tfrtrain') # image, label = read_and_decode('train.tfrecords') # batch_image, batch_label = get_batch(image, label, 30) #连接网络 网络训练 net = network() inf = net.inference(x, dropout) loss = net.sorfmax_loss(inf,y) opti = net.optimer(loss) correct_pred = tf.equal(tf.argmax(inf, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # #定义测试 create_test_record('/Users/hanjiarong/Documents/testdata/tfrtest') # image_t, label_t = read_and_decode('test.tfrecords') # batch_test_image, batch_test_label = get_test_batch(image_t, label_t, 50) # # #生成测试 image, label = read_and_decode('train.tfrecords') batch_image, batch_label = get_batch(image, label, 1) # val, l = session.run([batch_image, batch_label]) # print(val.shape, l) with tf.Session() as session: init = tf.initialize_all_variables() session.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) max_iter = 100000 iter = 1 print("begin1") while iter * 30 < max_iter: # loss_np, _, label_np, image_np, inf_np = session.run([loss, opti, batch_label, batch_image, inf]) session.run(opti, feed_dict={x: session.run(batch_image), y: session.run(batch_label), keep_drop: dropout}) print("begin6") if iter % 10 == 0: loss, acc = session.run([loss, accuracy], feed_dict={x: batch_image, y: batch_label, keep_drop: 1.}) print("Iter " + str(iter * 30) + ", Minibatch Loss= " + \ "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)) iter += 1 print("Optimization Finished!") image, label = read_and_decode('test.tfrecords') batch_test_image, batch_test_label = get_batch(image, label, 2) img_test, lab_test = session.run([batch_test_image, batch_test_label]) test_accuracy = session.run(accuracy, feed_dict={x: img_test, y: lab_test, keep_drop: 1.}) print("Testing Accuracy:", test_accuracy) ```
求各位大神看一下哪里错了,怎么改。
错误: ValueError: setting an array element with a sequence. import tensorflow as tf import scipy.io as sio import numpy as np import matplotlib.image as mpimg import pickle as cp import matplotlib.pyplot as plt def get_batch(image, label, batch_size, now_batch, total_batch): if now_batch < total_batch-1: image_batch = image[now_batch*batch_size:(now_batch+1)*batch_size] label_batch = label[now_batch*batch_size:(now_batch+1)*batch_size] else: image_batch = image[now_batch*batch_size:] label_batch = label[now_batch*batch_size:] return image_batch,label_batch label_tain = list(sio.loadmat('dataset/corel5k_train_annot.mat')['annot1']) #加载文件 label_test = list(sio.loadmat('dataset/corel5k_test_annot.mat')['annot2']) test_img = [] with open('dataset/corel5k_test_list.txt') as f: for i in f.readlines(): test_img += [mpimg.imread('dataset/%s.jpeg'%i.strip())] cp.dump(test_img,open("test.pkl","wb"))#一种保存列表的方式 train_img = [] with open('dataset/corel5k_train_list.txt') as f: for i in f.readlines(): train_img += [mpimg.imread('dataset/%s.jpeg'%i.strip())] cp.dump(train_img,open("train.pkl","wb")) length=len(train_img) for e in range(1,length+1): train_img[e-1] = tf.image.resize_images(train_img[e-1], [128,192], method=0) train_img[e-1] = tf.image.per_image_standardization(train_img[e-1]) def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) ########卷积层、池化层接下来重复使用的,分别定义创建函数######## # tf.nn.conv2d是TensorFlow中的2维卷积函数 def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') # 使用2*2的最大池化 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME') x = tf.placeholder(tf.float32,[None,128,192,3]) y_ = tf.placeholder(tf.float32,[None,260]) W_conv1 = weight_variable([3, 3, 3, 32]) # 用conv2d函数进行卷积操作,加上偏置 b_conv1 = bias_variable([32]) # 把x_image和权值向量进行卷积,加上偏置项,然后应用ReLU激活函数, h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) #tf.global_variables_initializer().run() # 对卷积的输出结果进行池化操作 h_pool1 = max_pool_2x2(h_conv1) image_batch,label_batch = get_batch(train_img, label_train,5,0,9) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(h_pool1,feed_dict={x:image_batch,y_:label_batch})
深度学习图片识别循环停止?
最近在跑深度学习的inceptionV3的时候偶尔会遇到一问题,就是代码在运行到某个时间点时,就停止迭代运算,不知道为什么? ![图片说明](https://img-ask.csdn.net/upload/201811/11/1541904283_461011.png) 上面图是个例子,假设运行到291的step的时候停止了,不在继续运算,但是CPU和GPU是满载的。 下面是代码: ``` # coding=utf-8 import tensorflow as tf import numpy as np import pdb import os from datetime import datetime import slim.inception_model as inception_v3 from create_tf_record import * import tensorflow.contrib.slim as slim labels_nums = 7 # 类别个数 batch_size = 64 # resize_height = 299 # 指定SSS存储图片高度 resize_width = 299 # 指定存储图片宽度 depths = 3 data_shape = [batch_size, resize_height, resize_width, depths] # 定义input_images为图片数据 input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input') # 定义input_labels为labels数据 # input_labels = tf.placeholder(dtype=tf.int32, shape=[None], name='label') input_labels = tf.placeholder(dtype=tf.int32, shape=[None, labels_nums], name='label') # 定义dropout的概率 keep_prob = tf.placeholder(tf.float32, name='keep_prob') is_training = tf.placeholder(tf.bool, name='is_training') #config = tf.ConfigProto() #config = tf.ConfigProto() #config.gpu_options.allow_growth = True #tf.Session(config = config) #tf.Session(config=tf.ConfigProto(allow_growth=True)) def net_evaluation(sess, loss, accuracy, val_images_batch, val_labels_batch, val_nums): val_max_steps = int(val_nums / batch_size) val_losses = [] val_accs = [] for _ in range(val_max_steps): val_x, val_y = sess.run([val_images_batch, val_labels_batch]) # print('labels:',val_y) # val_loss = sess.run(loss, feed_dict={x: val_x, y: val_y, keep_prob: 1.0}) # val_acc = sess.run(accuracy,feed_dict={x: val_x, y: val_y, keep_prob: 1.0}) val_loss, val_acc = sess.run([loss, accuracy], feed_dict={input_images: val_x, input_labels: val_y, keep_prob: 1.0, is_training: False}) val_losses.append(val_loss) val_accs.append(val_acc) mean_loss = np.array(val_losses, dtype=np.float32).mean() mean_acc = np.array(val_accs, dtype=np.float32).mean() return mean_loss, mean_acc def step_train(train_op, loss, accuracy, train_images_batch, train_labels_batch, train_nums, train_log_step, val_images_batch, val_labels_batch, val_nums, val_log_step, snapshot_prefix, snapshot): ''' 循环迭代训练过程 :param train_op: 训练op :param loss: loss函数 :param accuracy: 准确率函数 :param train_images_batch: 训练images数据 :param train_labels_batch: 训练labels数据 :param train_nums: 总训练数据 :param train_log_step: 训练log显示间隔 :param val_images_batch: 验证images数据 :param val_labels_batch: 验证labels数据 :param val_nums: 总验证数据 :param val_log_step: 验证log显示间隔 :param snapshot_prefix: 模型保存的路径 :param snapshot: 模型保存间隔 :return: None ''' # 初始化 #init = tf.global_variables_initializer() saver = tf.train.Saver() max_acc = 0.0 #ckpt = tf.train.get_checkpoint_state('D:/can_test/inception v3/') #saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path + '.meta') #tf.reset_default_graph() with tf.Session() as sess: #sess.run(tf.global_variables_initializer())#恢复训练用 #saver = tf.train.import_meta_graph('D://can_test/inception v3/best_models_2_0.7500.ckpt.meta')#恢复训练 #saver.restore(sess, tf.train.latest_checkpoint('D://can_test/inception v3/'))#恢复训练 sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(max_steps + 1): batch_input_images, batch_input_labels = sess.run([train_images_batch, train_labels_batch]) _, train_loss = sess.run([train_op, loss], feed_dict={input_images: batch_input_images, input_labels: batch_input_labels, keep_prob: 0.5, is_training: True}) # train测试(这里仅测试训练集的一个batch) if i % train_log_step == 0: train_acc = sess.run(accuracy, feed_dict={input_images: batch_input_images, input_labels: batch_input_labels, keep_prob: 1.0, is_training: False}) print( "%s: Step [%d] train Loss : %f, training accuracy : %g" % ( datetime.now(), i, train_loss, train_acc) ) # val测试(测试全部val数据) if i % val_log_step == 0: mean_loss, mean_acc = net_evaluation(sess, loss, accuracy, val_images_batch, val_labels_batch, val_nums) print( "%s: Step [%d] val Loss : %f, val accuracy : %g" % (datetime.now(), i, mean_loss, mean_acc) ) # 模型保存:每迭代snapshot次或者最后一次保存模型 if i == max_steps: print('-----save:{}-{}'.format(snapshot_prefix, i)) saver.save(sess, snapshot_prefix, global_step=i) # 保存val准确率最高的模型 if mean_acc > max_acc and mean_acc > 0.90: max_acc = mean_acc path = os.path.dirname(snapshot_prefix) best_models = os.path.join(path, 'best_models_{}_{:.4f}.ckpt'.format(i, max_acc)) print('------save:{}'.format(best_models)) saver.save(sess, best_models) coord.request_stop() coord.join(threads) def train(train_record_file, train_log_step, train_param, val_record_file, val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix): ''' :param train_record_file: 训练的tfrecord文件 :param train_log_step: 显示训练过程log信息间隔 :param train_param: train参数 :param val_record_file: 验证的tfrecord文件 :param val_log_step: 显示验证过程log信息间隔 :param val_param: val参数 :param labels_nums: labels数 :param data_shape: 输入数据shape :param snapshot: 保存模型间隔 :param snapshot_prefix: 保存模型文件的前缀名 :return: ''' [base_lr, max_steps] = train_param [batch_size, resize_height, resize_width, depths] = data_shape # 获得训练和测试的样本数 train_nums = get_example_nums(train_record_file) val_nums = get_example_nums(val_record_file) print('train nums:%d,val nums:%d' % (train_nums, val_nums)) # 从record中读取图片和labels数据 # train数据,训练数据一般要求打乱顺序shuffle=True train_images, train_labels = read_records(train_record_file, resize_height, resize_width, type='normalization') train_images_batch, train_labels_batch = get_batch_images(train_images, train_labels, batch_size=batch_size, labels_nums=labels_nums, one_hot=True, shuffle=True) # val数据,验证数据可以不需要打乱数据 val_images, val_labels = read_records(val_record_file, resize_height, resize_width, type='normalization') val_images_batch, val_labels_batch = get_batch_images(val_images, val_labels, batch_size=batch_size, labels_nums=labels_nums, one_hot=True, shuffle=False) # Define the model: with slim.arg_scope(inception_v3.inception_v3_arg_scope()): out, end_points = inception_v3.inception_v3(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=keep_prob, is_training=is_training) # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了 tf.losses.softmax_cross_entropy(onehot_labels=input_labels, logits=out) # 添加交叉熵损失loss=1.6 # slim.losses.add_loss(my_loss) loss = tf.losses.get_total_loss(add_regularization_losses=True) # 添加正则化损失loss=2.2 accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)), tf.float32)) # Specify the optimization scheme: optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr) # global_step = tf.Variable(0, trainable=False) # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9) # # optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9) # # train_tensor = optimizer.minimize(loss, global_step) # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step) # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数, # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新 # 通过`tf.get_collection`获得所有需要更新的`op` update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练 with tf.control_dependencies(update_ops): # create_train_op that ensures that when we evaluate it to get the loss, # the update_ops are done and the gradient updates are computed. # train_op = slim.learning.create_train_op(total_loss=loss,optimizer=optimizer) train_op = slim.learning.create_train_op(total_loss=loss, optimizer=optimizer) # 循环迭代过程 step_train(train_op, loss, accuracy, train_images_batch, train_labels_batch, train_nums, train_log_step, val_images_batch, val_labels_batch, val_nums, val_log_step, snapshot_prefix, snapshot) if __name__ == '__main__': train_record_file = '/home/lab/new_jeremie/train.tfrecords' val_record_file = '/home/lab/new_jeremie/val.tfrecords' #train_record_file = 'D://cancer_v2/data/cancer/train.tfrecords' #val_record_file = 'D://val.tfrecords' train_log_step = 1 base_lr = 0.01 # 学习率 max_steps = 100000 # 迭代次数 train_param = [base_lr, max_steps] val_log_step = 1 snapshot = 2000 # 保存文件间隔 snapshot_prefix = './v3model.ckpt' train(train_record_file=train_record_file, train_log_step=train_log_step, train_param=train_param, val_record_file=val_record_file, val_log_step=val_log_step, #val_log_step=val_log_step, labels_nums=labels_nums, data_shape=data_shape, snapshot=snapshot, snapshot_prefix=snapshot_prefix) ```
TensorFlow卷积神经网络实现,图片与对应标签的拟合。一直显示卡在正在运行。
#python3代码 #TensorFlow卷积神经网络实现,图片与对应标签的拟合 #程序不报错,一直显示正在运行 #请问错在哪里? #数据集 https://pan.baidu.com/s/1Z7_ULAW3LFrUk4WkHC0s3Q import os import numpy as np from PIL import Image import tensorflow as tf import matplotlib.pyplot as plt #-------------获取数据集,构造批数据:batch----------------------------------------- def get_files(file_dir): pictures = [] numbers = [] for file in os.listdir(file_dir): # 7000.jpg name = file.split(sep='.') pictures.append(file_dir+"\\"+file) numbers.append(name[0]) print('There are %d pictures'%(len(pictures))) temp = np.array([pictures,numbers]) temp = temp.transpose() np.random.shuffle(temp) pictures_list = list(temp[:,0]) numbers_list = list(temp[:,1]) #numbers_list = [int(float(i)) for i in numbers_list] return pictures_list,numbers_list #返回两个list def get_batch(image,label,image_W,image_H,batch_size,capacity): # image = pictures label = numbers image = tf.cast(image,tf.string) label = tf.cast(label,tf.int32) #tf.cast()用来做类型转换 input_queue = tf.train.slice_input_producer([image,label]) #加入队列 label = input_queue[1] image_contents = tf.read_file(input_queue[0]) image = tf.image.decode_jpeg(image_contents,channels=3) #jpeg或者jpg格式都用decode_jpeg函数,其他格式可以去查看官方文档 image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H) #resize image = tf.image.per_image_standardization(image) #对resize后的图片进行标准化处理 image_batch,label_batch = tf.train.batch([image,label],batch_size = batch_size,num_threads=16,capacity = capacity) label_batch = tf.reshape(label_batch,[batch_size]) return image_batch,label_batch #获取两个batch,两个batch即为传入神经网络的数据 #-----------卷积神经网络节点,搭建卷积神经网络---------------------------------------------------- def conv_op(input_op,name,kh,kw,n_out,dh,dw): n_in = input_op.get_shape()[-1].value with tf.name_scope(name) as scope: kernel=tf.get_variable(scope+"w", shape=[kh,kw,n_in,n_out],dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer_conv2d()) conv=tf.nn.conv2d(input_op,kernel,(1,dh,dw,1),padding='SAME') bias_init_val=tf.constant(0.0,shape=[n_out],dtype=tf.float32) biases=tf.Variable(bias_init_val,trainable=True,name='b') z=tf.nn.bias_add(conv,biases) activation=tf.nn.relu(z,name=scope) return activation def fc_op(input_op,name,n_out): n_in=input_op.get_shape()[-1].value with tf.name_scope(name) as scope: kernel = tf.get_variable(scope+"w",shape=[n_in,n_out], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) biases=tf.Variable(tf.constant(0.1, shape=[n_out],dtype=tf.float32),name='b') activation=tf.nn.relu_layer(input_op,kernel,biases,name=scope) return activation def fc_end_layer(inputs,in_size,out_size,activation_function=None): Weights=tf.Variable(tf.random_normal([in_size,out_size])) biases=tf.Variable(tf.zeros([1,out_size])+0.1) 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 def mpool_op(input_op,name,kh,kw,dh,dw): return tf.nn.max_pool(input_op,ksize=[1,kh,kw,1], strides=[1,dh,dw,1],padding='SAME',name=name) def inference_op(input_op,keep_prob): conv1_1=conv_op(input_op,name="conv1_1",kh=3,kw=3,n_out=64,dh=1, dw=1) conv1_2=conv_op(conv1_1,name="conv1_2",kh=3,kw=3,n_out=64,dh=1, dw=1) pool1=mpool_op(conv1_2,name="pool1",kh=2,kw=2,dw=2,dh=2) conv2_1=conv_op(pool1,name="conv2_1",kh=3,kw=3,n_out=128,dh=1, dw=1) conv2_2=conv_op(conv2_1,name="conv2_2",kh=3,kw=3,n_out=128,dh=1, dw=1) pool2=mpool_op(conv2_2,name="pool2",kh=2,kw=2,dh=2,dw=2) conv3_1=conv_op(pool2,name="conv3_1",kh=3,kw=3,n_out=256,dh=1, dw=1) conv3_2=conv_op(conv3_1,name="conv3_2",kh=3,kw=3,n_out=256,dh=1, dw=1) conv3_3=conv_op(conv3_2,name="conv3_3",kh=3,kw=3,n_out=256,dh=1, dw=1) pool3=mpool_op(conv3_3,name="pool3",kh=2,kw=2,dh=2,dw=2) conv4_1=conv_op(pool3,name="conv4_1",kh=3,kw=3,n_out=512,dh=1, dw=1) conv4_2=conv_op(conv4_1,name="conv4_2",kh=3,kw=3,n_out=512,dh=1, dw=1) conv4_3=conv_op(conv4_2,name="conv4_3",kh=3,kw=3,n_out=512,dh=1, dw=1) pool4=mpool_op(conv4_3,name="pool4",kh=2,kw=2,dh=2,dw=2) conv5_1=conv_op(pool4,name="conv5_1",kh=3,kw=3,n_out=512,dh=1, dw=1) conv5_2=conv_op(conv5_1,name="conv5_2",kh=3,kw=3,n_out=512,dh=1, dw=1) conv5_3=conv_op(conv5_2,name="conv5_3",kh=3,kw=3,n_out=512,dh=1, dw=1) pool5=mpool_op(conv5_3,name="pool5",kh=2,kw=2,dw=2,dh=2) shp=pool5.get_shape() flattened_shape=shp[1].value*shp[2].value*shp[3].value resh1=tf.reshape(pool5,[-1,flattened_shape],name="resh1") fc6=fc_op(resh1,name="fc6",n_out=4096) fc6_drop=tf.nn.dropout(fc6,keep_prob,name="fc6_drop") fc7=fc_op(fc6_drop,name="fc7",n_out=4096) fc7_drop=tf.nn.dropout(fc7,keep_prob,name="fc7_drop") fc8=fc_op(fc7_drop,name="fc8",n_out=1000) prediction=fc_end_layer(fc8,1000,1,activation_function=None) return prediction #---------构造损失节点:loss 优化节点:optimizer 训练节点:train--------------------------------------- x=tf.placeholder(tf.float32,[None,28,28,3]) y=tf.placeholder(tf.float32,[None,1]) prediction = inference_op(x,0.5) loss = tf.reduce_mean(tf.square(y - prediction)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) #--------运行网络-------------------------------------------------- IMG_W = 28 IMG_H = 28 BATCH_SIZE = 4 CAPACITY = 24 MAX_STEP = 100 learning_rate = 0.0005 train_dir = 'D:\\VGGNet-16\\fatigue_cycle_picture' pictures_list,numbers_list = get_files(train_dir) image_batch,label_batch = get_batch(pictures_list,numbers_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY) #label_batch=tf.to_float(label_batch) sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(100): print(i) print(sess.run(image_batch)) sess.run(train,feed_dict={x:image_batch.eval(session=sess),y:label_batch.eval(session=sess)}) sess.close()
相见恨晚的超实用网站
相见恨晚的超实用网站 持续更新中。。。
字节跳动视频编解码面经
三四月份投了字节跳动的实习(图形图像岗位),然后hr打电话过来问了一下会不会opengl,c++,shador,当时只会一点c++,其他两个都不会,也就直接被拒了。 七月初内推了字节跳动的提前批,因为内推没有具体的岗位,hr又打电话问要不要考虑一下图形图像岗,我说实习投过这个岗位不合适,不会opengl和shador,然后hr就说秋招更看重基础。我当时想着能进去就不错了,管他哪个岗呢,就同意了面试...
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它是一个过程,是一个不断累积、不断沉淀、不断总结、善于传达自己的个人见解以及乐于分享的过程。
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 free -m 其中:m表示兆,也可以用g,注意都要小写 Men:表示物理内存统计 total:表示物理内存总数(total=used+free) use...
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
python学习方法总结(内附python全套学习资料)
不要再问我python好不好学了 我之前做过半年少儿编程老师,一个小学四年级的小孩子都能在我的教学下独立完成python游戏,植物大战僵尸简单版,如果要肯花时间,接下来的网络开发也不是问题,人工智能也可以学个调包也没啥问题。。。。。所以python真的是想学就一定能学会的!!!! --------------------华丽的分割线-------------------------------- ...
python 简易微信实现(注册登录+数据库存储+聊天+GUI+文件传输)
socket+tkinter详解+简易微信实现 历经多天的努力,查阅了许多大佬的博客后终于实现了一个简易的微信O(∩_∩)O~~ 简易数据库的实现 使用pands+CSV实现数据库框架搭建 import socket import threading from pandas import * import pymysql import csv # 创建DataFrame对象 # 存储用户数据的表(...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发...
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 演示地点演示 html代码如下` music 这个年纪 七月的风 音乐 ` 然后就是css`*{ margin: 0; padding: 0; text-decoration: none; list-...
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。
数据库优化 - SQL优化
以实际SQL入手,带你一步一步走上SQL优化之路!
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...
通俗易懂地给女朋友讲:线程池的内部原理
餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。 也可以说是一个小型人工智障。 知识可以运用在不同地方,不一定非是天气预报。
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
Python实例大全(基于Python3.7.4)
博客说明: 这是自己写的有关python语言的一篇综合博客。 只作为知识广度和编程技巧学习,不过于追究学习深度,点到即止、会用即可。 主要是基础语句,如三大控制语句(顺序、分支、循环),随机数的生成,数据类型的区分和使用; 也会涉及常用的算法和数据结构,以及面试题相关经验; 主体部分是针对python的数据挖掘和数据分析,主要先攻爬虫方向:正则表达式匹配,常用数据清洗办法,scrapy及其他爬虫框架,数据存储方式及其实现; 最后还会粗略涉及人工智能领域,玩转大数据与云计算、进行相关的预测和分析。
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹...
面试官:你连RESTful都不知道我怎么敢要你?
干货,2019 RESTful最贱实践
刷了几千道算法题,这些我私藏的刷题网站都在这里了!
遥想当年,机缘巧合入了 ACM 的坑,周边巨擘林立,从此过上了"天天被虐似死狗"的生活… 然而我是谁,我可是死狗中的战斗鸡,智力不够那刷题来凑,开始了夜以继日哼哧哼哧刷题的日子,从此"读题与提交齐飞, AC 与 WA 一色 ",我惊喜的发现被题虐既刺激又有快感,那一刻我泪流满面。这么好的事儿作为一个正直的人绝不能自己独享,经过激烈的颅内斗争,我决定把我私藏的十几个 T 的,阿不,十几个刷题网...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看...
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
JavaScript 为什么能活到现在?
作者 | 司徒正美 责编 |郭芮 出品 | CSDN(ID:CSDNnews) JavaScript能发展到现在的程度已经经历不少的坎坷,早产带来的某些缺陷是永久性的,因此浏览器才有禁用JavaScript的选项。甚至在jQuery时代有人问出这样的问题,jQuery与JavaScript哪个快?在Babel.js出来之前,发明一门全新的语言代码代替JavaScript...
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
Nginx 原理和架构
Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器。Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求,主要负责调度工作进程:加载配置、启动工作进程及非停升级。worker 进程负责处...
致 Python 初学者
欢迎来到“Python进阶”专栏!来到这里的每一位同学,应该大致上学习了很多 Python 的基础知识,正在努力成长的过程中。在此期间,一定遇到了很多的困惑,对未来的学习方向感到迷茫。我非常理解你们所面临的处境。我从2007年开始接触 python 这门编程语言,从2009年开始单一使用 python 应对所有的开发工作,直至今天。回顾自己的学习过程,也曾经遇到过无数的困难,也曾经迷茫过、困惑过。开办这个专栏,正是为了帮助像我当年一样困惑的 Python 初学者走出困境、快速成长。希望我的经验能真正帮到你
Python 编程开发 实用经验和技巧
Python是一门很灵活的语言,也有很多实用的方法,有时候实现一个功能可以用多种方法实现,我这里总结了一些常用的方法和技巧,包括小数保留指定位小数、判断变量的数据类型、类方法@classmethod、制表符中文对齐、遍历字典、datetime.timedelta的使用等,会持续更新......
吐血推荐珍藏的Visual Studio Code插件
作为一名Java工程师,由于工作需要,最近一个月一直在写NodeJS,这种经历可以说是一部辛酸史了。好在有神器Visual Studio Code陪伴,让我的这段经历没有更加困难。眼看这段经历要告一段落了,今天就来给大家分享一下我常用的一些VSC的插件。 VSC的插件安装方法很简单,只需要点击左侧最下方的插件栏选项,然后就可以搜索你想要的插件了。 下面我们进入正题 Material Theme ...
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
相关热词 c#中dns类 c#合并的excel c# implicit c#怎么保留3个小数点 c# 串口通信、 网络调试助手c# c# 泛型比较大小 c#解压分卷问题 c#启动居中 c# 逻辑或运算符
立即提问