tensorflow学习1 softmax实现

from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_DATA/",one_hot=True)
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
In [2]:
print(mnist.train.images.shape,mnist.train.labels.shape)
print(mnist.test.images.shape,mnist.test.labels.shape)
print(mnist.validation.images.shape,mnist.validation.labels.shape)
(55000, 784) (55000, 10)
(10000, 784) (10000, 10)
(5000, 784) (5000, 10)
In [3]:
#creat new session
import tensorflow as tf
sess=tf.InteractiveSession()#interactivesession表示这是当前运行的session
x=tf.placeholder(tf.float32,[None,784])#placeholder表示输入数据,None表示不限制个数,float表示输入的类型
In [4]:
W=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))#Variable用来存储模型训练的参数
In [5]:
#接下来实现softmax
y=tf.nn.softmax(tf.matmul(x,W)+b)
In [6]:
#定义loss
y_=tf.placeholder(tf.float32,[None,10])
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
#reduce_mean表示对每一个batch数据求均值,reduce_sum是求和
In [7]:
#定义优化算法
train_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#学习速率是0.5的FSGD算法
In [8]:
#全局优化初始器
tf.global_variables_initializer().run()
In [9]:
for i in range(1000):
    batch_xs,batch_ys=mnist.train.next_batch(100)
    train_step.run({x:batch_xs,y_:batch_ys})
    #以上就训练完成
    #下面查看训练结果
    correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    #求准确率用correct的bool值先转换成float型
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
0.4018
0.4394
0.5652
0.5473
0.4915
0.6309
0.7128
0.7356
0.7173
0.7626
0.8126
0.7761
0.7731
0.7912
0.8267
0.7635
0.8147
0.8117
0.8265
0.8504
0.8429
0.8576
0.8473
0.861
0.8591
0.8603
0.8601
0.8645
0.8589
0.8042
0.8095
0.8208
0.8137
0.8099
0.8598
0.861
0.872
0.8557
0.8649
0.8789
0.8663
0.8728
0.8732
0.8666
0.8684
0.8799
0.8798
0.8804
0.8754
0.8814
0.8706
0.8763
0.8823
0.8764
0.886
0.8849
0.8803
0.8849
0.8764
0.8802
0.8763
0.8789
0.8751
0.8806
0.8806
0.8742
0.8823
0.8839
0.8797
0.8795
0.8824
0.8838
0.8861
0.8821
0.8824
0.8805
0.8638
0.8787
0.8762
0.8884
0.8874
0.8781
0.89
0.8887
0.8874
0.8944
0.8867
0.883
0.8862
0.8917
0.8897
0.8907
0.8925
0.877
0.8899
0.8842
0.8928
0.8949
0.8921
0.8858
0.8852
0.8893
0.8919
0.897
0.8939
0.886
0.8901
0.8944
0.8773
0.8906
0.8903
0.8904
0.8945
0.8972
0.8911
0.8907
0.8946
0.8868
0.8894
0.8926
0.8872
0.8911
0.8974
0.8932
0.8916
0.8811
0.8932
0.8944
0.8934
0.8941
0.8947
0.8955
0.8903
0.892
0.8916
0.899
0.9007
0.9001
0.8917
0.899
0.8797
0.9003
0.8997
0.8962
0.8882
0.8995
0.8933
0.8934
0.8985
0.8991
0.8976
0.9003
0.8929
0.8947
0.8991
0.899
0.8885
0.9014
0.9019
0.899
0.9007
0.8997
0.8993
0.8982
0.9016
0.8991
0.8979
0.9005
0.8991
0.9043
0.9027
0.9033
0.9021
0.9028
0.9047
0.9025
0.9046
0.8992
0.9018
0.9018
0.9013
0.8976
0.8999
0.8999
0.8888
0.8958
0.8941
0.8913
0.9025
0.9009
0.896
0.9013
0.8994
0.9055
0.9029
0.9023
0.9026
0.904
0.9052
0.9065
0.9055
0.9061
0.9081
0.905
0.9027
0.9036
0.9007
0.9043
0.8996
0.9044
0.9041
0.9081
0.9036
0.902
0.9024
0.9044
0.9068
0.907
0.9071
0.901
0.901
0.9056
0.9067
0.9069
0.9042
0.9034
0.9042
0.9072
0.908
0.9045
0.9025
0.91
0.9013
0.9076
0.9075
0.909
0.9095
0.9077
0.9073
0.9115
0.9076
0.9041
0.9094
0.906
0.9053
0.901
0.9055
0.9044
0.9094
0.9081
0.9044
0.9066
0.9084
0.9078
0.9076
0.9089
0.9072
0.9015
0.9032
0.9006
0.9068
0.905
0.9068
0.9052
0.9043
0.9038
0.9043
0.9068
0.9084
0.9078
0.9063
0.9044
0.9034
0.9084
0.9088
0.9104
0.9087
0.9022
0.9057
0.9032
0.9058
0.9108
0.9101
0.9118
0.9088
0.9098
0.9104
0.909
0.9112
0.9123
0.9062
0.9118
0.9107
0.9075
0.9086
0.9114
0.9027
0.9016
0.9061
0.9082
0.9083
0.9099
0.9073
0.9107
0.9096
0.9081
0.9044
0.9034
0.9062
0.9103
0.9074
0.9088
0.9034
0.9019
0.9064
0.9099
0.9099
0.9101
0.9085
0.9095
0.9095
0.9093
0.8953
0.9034
0.9025
0.9107
0.909
0.9087
0.9114
0.9082
0.9041
0.9085
0.9052
0.9107
0.9095
0.91
0.9108
0.9086
0.9055
0.9032
0.9094
0.9081
0.9078
0.9125
0.9084
0.9117
0.9095
0.905
0.9042
0.9027
0.9056
0.9067
0.9108
0.9144
0.911
0.9071
0.9114
0.912
0.9115
0.9107
0.9087
0.9106
0.9098
0.9027
0.9075
0.9127
0.9124
0.9089
0.9077
0.9078
0.9105
0.8942
0.9044
0.9056
0.9074
0.9108
0.9098
0.9132
0.9078
0.9085
0.9075
0.9048
0.9092
0.9054
0.9117
0.9122
0.9113
0.9111
0.9091
0.9098
0.91
0.9124
0.9106
0.9022
0.9095
0.9132
0.9094
0.9137
0.9106
0.9125
0.9127
0.9098
0.9112
0.9139
0.9063
0.9071
0.9118
0.9144
0.9133
0.9078
0.9143
0.9091
0.9143
0.9158
0.9138
0.9151
0.9135
0.9096
0.9121
0.9016
0.911
0.9088
0.9094
0.9144
0.9137
0.9119
0.9154
0.915
0.912
0.9158
0.9121
0.9109
0.9129
0.9139
0.9133
0.9114
0.9127
0.9144
0.913
0.9146
0.9118
0.9142
0.9143
0.9156
0.9141
0.9142
0.9146
0.9145
0.9109
0.9142
0.9166
0.9155
0.9162
0.9137
0.9147
0.9158
0.9144
0.9133
0.9151
0.9115
0.9146
0.9142
0.913
0.9146
0.9135
0.9179
0.9156
0.9108
0.9154
0.917
0.9149
0.9177
0.9135
0.9152
0.9153
0.9154
0.9155
0.9114
0.9149
0.9152
0.9132
0.9132
0.9137
0.9123
0.9153
0.9138
0.917
0.9167
0.9177
0.9161
0.9144
0.9147
0.9146
0.9171
0.9166
0.9159
0.9159
0.9158
0.9144
0.9153
0.9162
0.9152
0.9172
0.9148
0.9151
0.9125
0.915
0.9132
0.9128
0.9163
0.914
0.9124
0.9146
0.9157
0.9119
0.9146
0.9152
0.916
0.913
0.9161
0.9137
0.9136
0.9151
0.915
0.9156
0.9166
0.917
0.9159
0.9153
0.9136
0.9142
0.9123
0.9137
0.9147
0.9118
0.9099
0.9122
0.9122
0.9155
0.9179
0.9172
0.9158
0.9163
0.914
0.9138
0.9136
0.9144
0.9119
0.914
0.9138
0.9135
0.9136
0.9135
0.9098
0.9138
0.9171
0.9171
0.9157
0.9177
0.9145
0.9175
0.9148
0.9157
0.9165
0.9177
0.9144
0.9162
0.9162
0.9172
0.9153
0.9138
0.9137
0.9152
0.9171
0.918
0.9161
0.9159
0.916
0.9137
0.9148
0.9136
0.9158
0.9129
0.9155
0.9173
0.9173
0.9176
0.9178
0.9177
0.9134
0.9177
0.9165
0.9117
0.91
0.9155
0.9164
0.916
0.912
0.9106
0.9174
0.9187
0.9142
0.914
0.9156
0.9142
0.9145
0.9118
0.9149
0.9145
0.9141
0.9096
0.9147
0.9133
0.9136
0.9136
0.9091
0.9117
0.916
0.9129
0.9155
0.9167
0.9162
0.9178
0.917
0.9168
0.9159
0.9173
0.9145
0.9159
0.9107
0.9162
0.9158
0.9144
0.9122
0.9095
0.9137
0.914
0.9164
0.9145
0.9151
0.9155
0.9161
0.9169
0.9148
0.9131
0.9125
0.9144
0.9167
0.9157
0.9171
0.9158
0.9116
0.9107
0.914
0.9167
0.9175
0.9125
0.9133
0.9157
0.9144
0.9153
0.917
0.9132
0.9125
0.9153
0.9161
0.9156
0.9173
0.9177
0.9168
0.9143
0.9182
0.9136
0.9139
0.9161
0.9178
0.9183
0.9144
0.9163
0.9171
0.9186
0.9187
0.9192
0.9181
0.9159
0.9175
0.9098
0.9173
0.9192
0.9163
0.92
0.9178
0.9154
0.9166
0.9153
0.9175
0.9169
0.916
0.9185
0.9187
0.9171
0.9177
0.9162
0.9125
0.9126
0.9187
0.9177
0.9155
0.9153
0.9157
0.9149
0.917
0.9166
0.9096
0.9136
0.9166
0.9161
0.9167
0.915
0.915
0.9168
0.9179
0.9147
0.9167
0.9152
0.914
0.9173
0.9142
0.9137
0.9153
0.9146
0.9161
0.9165
0.9139
0.9149
0.9144
0.9153
0.9108
0.9165
0.9155
0.9149
0.9104
0.913
0.913
0.9141
0.9167
0.9182
0.9143
0.9162
0.9178
0.918
0.9145
0.9172
0.9163
0.9176
0.9162
0.9157
0.9144
0.9181
0.9162
0.9176
0.9146
0.9163
0.9175
0.9135
0.9167
0.9182
0.9166
0.9165
0.9165
0.9162
0.9164
0.9156
0.9166
0.9157
0.9163
0.9171
0.9178
0.9117
0.9184
0.9144
0.9181
0.9146
0.9136
0.9123
0.9114
0.9131
0.9163
0.9158
0.9147
0.9126
0.9174
0.917
0.9188
0.9191
0.9193
0.9076
0.9179
0.915
0.9119
0.9174
0.9182
0.9171
0.9158
0.9187
0.9159
0.9105
0.9169
0.9163
0.9145
0.9165
0.9136
0.9141
0.9173
0.9146
0.9171
0.9187
0.918
0.9171
0.9165
0.9086
0.9144
0.9149
0.9179
0.9174
0.9177
0.9177
0.9172
0.9183
0.9185
0.9201
0.9187
0.9179
0.9184
0.9152
0.9154
0.9181
0.915
0.9201
0.9205
0.9172
0.9158
0.917
0.9175
0.9156
0.9183
0.9167
0.9184
0.912
0.918
0.9166
0.9192
0.9202
0.918
0.9218
0.9187
0.9199
0.9199
0.9184
0.9197
0.9196
0.9188
0.9175
0.9165
0.9169
0.9202
0.9175
0.9172
0.9174
0.9175
0.9148
0.9145
0.9149
0.918
0.9169
0.9178
0.9127
0.9176
0.9188
0.9171
0.9164
0.9174
0.9176
0.9162
0.9176
0.9155
0.917
0.9185
0.916
0.9178
0.916
0.918
0.9177
0.9169
0.9189
0.9196
0.9153
0.9181
0.9181
0.9185
0.9158
0.9185
0.9158
0.9152
0.9191
0.918
0.9155
0.9177
0.9203
0.9188
0.919
0.9181
0.9151
0.9144
0.9158
0.917
0.9175
0.9175
0.9174
0.9156
0.9167
0.9146
0.9145
0.9175
0.915
0.908
0.9075
0.9157
0.9124
0.9197
0.9199
0.9203
0.9162
0.9194
0.9172
0.9186
0.9163
0.9167
0.9154
0.9183
0.9169
0.9145
0.9141
0.9121
0.9145
0.9156
0.9128
0.9146
0.9147
0.9156
0.918
0.9143
0.9172
0.92
0.9171
0.919
0.9163
0.917
0.9172
0.9147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow中,softmax函数是一种常用的激活函数,用于将神经网络的输出转化为概率分布。通过对输出进行指数运算和归一化处理,softmax函数可以将连续数值转化为相对概率。在使用softmax函数时,需要注意数值溢出的问题,可以通过对输入进行数值处理来避免溢出的可能性。Softmax回归本身可以作为一个学习算法来优化分类结果,但在TensorFlow中,softmax回归的参数被去掉了,它只是一层额外的处理层,将神经网络的输出变成一个概率分布。\[2\]\[3\]所以,当我们在TensorFlow中使用softmax函数时,可以将神经网络的输出通过softmax函数转化为概率分布,从而更好地理解和处理分类问题。\[1\] #### 引用[.reference_title] - *1* *2* [tensorflow学习softmax使用详解](https://blog.csdn.net/u013230189/article/details/82835717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [TensorFlow实现Softmax](https://blog.csdn.net/NOKIA2015/article/details/101836613)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值