简单的全连接神经网络(tensorflow实现)

简单的全连接神经网络,网络结构为2-2-1

代码如下:

  1.  #encoding='utf-8'  
  2.   
  3. """ 
  4.         created on 2018-08-10 
  5.         @author wt 
  6. """  
  7.   
  8. import tensorflow as tf  
  9. import numpy as np  
  10.   
  11. learning_rate = 0.01  
  12. n_input = 2  
  13. n_label = 1  
  14. n_hidden = 2  
  15.   
  16. x = tf.placeholder(tf.float32,[None,n_input])  
  17. y = tf.placeholder(tf.float32,[None,n_label])  
  18.   
  19. weights = {  
  20.     'h1':tf.Variable(tf.truncated_normal([n_input,n_hidden],stddev=0.1)),  
  21.     'h2':tf.Variable(tf.truncated_normal([n_hidden,n_label],stddev=0.1))  
  22. }  
  23. biases = {  
  24.     'h1':tf.Variable(tf.zeros([n_hidden])),  
  25.     'h2':tf.Variable(tf.zeros([n_label]))  
  26. }  
  27.   
  28. layer1 = tf.nn.relu(tf.add(tf.matmul(x,weights['h1']),biases['h1']))  
  29. y_pred = tf.nn.tanh(tf.add(tf.matmul(layer1,weights['h2']),biases['h2']))  
  30.   
  31. loss = tf.reduce_mean((y-y_pred)**2)  
  32. train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)  
  33.   
  34. x1 = [[0,0],[0,1],[1,0],[1,1]]  
  35. y1 = [[0],[1],[1],[0]]  
  36. x1 = np.array(x1).astype('float32')  
  37. y1 = np.array(y1).astype('int16')  
  38.   
  39.   
  40. with tf.Session() as sess:  
  41.     sess.run(tf.global_variables_initializer())  
  42.     for i in range(1000):  
  43.         sess.run(train_step,feed_dict={x:x1,y:y1})  
  44.   
  45.     print(sess.run(y_pred,feed_dict={x:x1}))  
  46.     print(sess.run(layer1,feed_dict={x:x1}))  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于TensorFlow的卷积神经网络(CNN)模型,包含两层全连接层: ```python import tensorflow as tf # 定义输入和输出 x = tf.placeholder(tf.float32, [None, 784]) y_true = tf.placeholder(tf.float32, [None, 10]) # 定义卷积层 x_image = tf.reshape(x, [-1,28,28,1]) conv1 = tf.layers.conv2d(inputs=x_image, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) # 定义第一层全连接层 pool1_flat = tf.reshape(pool1, [-1, 14 * 14 * 32]) dense1 = tf.layers.dense(inputs=pool1_flat, units=1024, activation=tf.nn.relu) dropout1 = tf.layers.dropout(inputs=dense1, rate=0.4) # 定义第二层全连接层 dense2 = tf.layers.dense(inputs=dropout1, units=10) y_pred = tf.nn.softmax(dense2) # 定义损失函数和优化器 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=dense2)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # 定义准确率 correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) ``` 该模型包含一个卷积层和两个全连接层。在卷积层中,我们使用 `tf.layers.conv2d()` 函数定义一个卷积层,同时使用 `tf.layers.max_pooling2d()` 函数定义一个最大池化层。在全连接层中,我们使用 `tf.layers.dense()` 函数定义一个全连接层,并使用 `tf.layers.dropout()` 函数定义一个Dropout层来防止过拟合。 在训练过程中,我们使用交叉熵作为损失函数,使用Adam优化器来最小化损失函数。我们还定义了准确率作为评估模型的指标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值