bp神经网络 mnist java_Java实现BP神经网络MNIST手写数字识别

【实例简介】

Java实现BP神经网络,内含BP神经网络类,采用MNIST数据集,包含服务器和客户端程序,可在服务器训练后使客户端直接使用训练结果,界面有画板,可以手写数字

【实例截图】

【核心代码】

Recognition

├── RecognitionClient

│   ├── bin

│   │   └── RecognitionClient

│   │   ├── Controller$1.class

│   │   ├── Controller$2.class

│   │   ├── Controller.class

│   │   ├── Model.class

│   │   └── View.class

│   └── src

│   └── RecognitionClient

│   ├── Controller.java

│   ├── Model.java

│   └── View.java

└── RecognitionServer

├── RecognitionServer.iml

├── bin

│   └── RecognitionServer

│   ├── BP.class

│   ├── CSVFileUtil.class

│   ├── Controller$1.class

│   ├── Controller$2.class

│   ├── Controller.class

│   ├── Model$1.class

│   ├── Model$2.class

│   ├── Model.class

│   ├── ThreadServer.class

│   └── View.class

├── mnist_train.csv

├── mnist_train_labels.csv

└── src

└── RecognitionServer

├── BP.java

├── CSVFileUtil.java

├── Controller.java

├── Model.java

├── ThreadServer.java

└── View.java

10 directories, 27 files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是示例代码: ```python import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 读取数据集 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 设置神经网络参数 n_input = 784 # 输入层节点数 n_hidden1 = 256 # 隐藏层1节点数 n_hidden2 = 128 # 隐藏层2节点数 n_output = 10 # 输出层节点数 # 定义占位符 x = tf.placeholder("float", [None, n_input]) y = tf.placeholder("float", [None, n_output]) # 定义权重和偏置 weights = { 'h1': tf.Variable(tf.random_normal([n_input, n_hidden1])), 'h2': tf.Variable(tf.random_normal([n_hidden1, n_hidden2])), 'out': tf.Variable(tf.random_normal([n_hidden2, n_output])) } biases = { 'b1': tf.Variable(tf.random_normal([n_hidden1])), 'b2': tf.Variable(tf.random_normal([n_hidden2])), 'out': tf.Variable(tf.random_normal([n_output])) } # 定义网络结构 def multilayer_perceptron(x, weights, biases): # 隐藏层1的输出 layer1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) layer1 = tf.nn.relu(layer1) # 隐藏层2的输出 layer2 = tf.add(tf.matmul(layer1, weights['h2']), biases['b2']) layer2 = tf.nn.relu(layer2) # 输出层的输出 out_layer = tf.matmul(layer2, weights['out']) + biases['out'] return out_layer # 构建模型 pred = multilayer_perceptron(x, weights, biases) # 定义损失函数和优化器 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost) # 初始化变量 init = tf.global_variables_initializer() # 训练模型 with tf.Session() as sess: sess.run(init) # 循环训练1000次 for epoch in range(1000): avg_cost = 0. total_batch = int(mnist.train.num_examples/batch_size) # 循环所有的batches for i in range(total_batch): batch_x, batch_y = mnist.train.next_batch(batch_size) # 运行优化器来更新权重和偏置 _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y}) avg_cost += c / total_batch # 每100次输出一下损失函数 if epoch % 100 == 0: print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)) print("Optimization Finished!") # 测试模型 correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) ``` 在上述代码中,我们使用 TensorFlow 框架实现了一个包含三层神经网络BP 神经网络,并使用 MNIST 数字数据集进行分类任务的训练和测试。其中,我们设置了输入层节点数为 784,隐藏层1节点数为 256,隐藏层2节点数为 128,输出层节点数为 10。我们使用 ReLU 作为激活函数,Adam 作为优化器,损失函数采用 softmax 交叉熵。最后,我们训练模型1000次,并测试模型的准确率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值