import tensorflow as tf import pandas as pd import numpy as np #生产数据集 D=2#X的特征个数 K=300 C=2#输出Y的几类 X=(np.random((K,D))*10)-5 Y_c=[int(x0*x0+x1*x1<9) for (x0,x1) in X] Y=pd.get_dummies(Y_c).values#one-hot
#构建网络模型 def get_weight(shape,reg): w=tf.Variable(tf.random_normal(shape),dtype=tf.float32) #如果进行正则化将损失值加入到loss中 tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(reg)(w)) return w
#开始训练 with tf.Session() as sess: init_op=tf.global_variables_initializervar() sess.run(init_op) fpr i in range(STEPS): start=(i*BATCH_SIZE)%k end=start+BATCH_SIZE sess.run(train_function,feed_dict={x:X[start:end],y:Y[start:end]}) if i%100==0: # # 计算当前数据损失值 loss_v = sess.run(data_loss,feed_dict={x:X,y:Y}) print("this is %dth step,the loss is %f"%(i,loss_v))