本文主要是介绍sofxmax sigmod,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import tensorflow as tf#此时,权值构成了一个矩阵,而非向量,每个“特征权值列"对应一个输出类别
W = tf.Variable(tf.zeros([4, 3]), name = "Weights")
#每个偏置也是如此,每个偏置对应一个输出类
b = tf.Variable(tf.zeros([3]), name = "bias")def inference(X):return tf.nn.softmax(combine_inputs(X))def loss(X, Y):return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=combine_inputs(X), labels=Y))
def read_csv(batch_size, file_name, record_defaults):filename_queue = tf.train.string_input_producer(["iris.csv"])reader = tf.TextLineReader()key, value = reader.read(filename_queue)decoded = tf.decode_csv(value, record_defaults=record_defaults) # 字符串(文本行)转换到指定默认值张量列元组,为每列设置数据类型return tf.train.shuffle_batch(decoded, batch_size=batch_size, capacity=batch_size * 50,min_after_dequeue=batch_size) # 读取文件,加载张量batch_size行
def inputs():sepal_length, sepal_width, petal_length, petal_width, label = read_csv(100, "iris.data", [[0.0],[0.0],[0.0],[0.0],[""]])label_number = tf.to_int32(tf.argmax(tf.to_int32(tf.stack([tf.equal(label,["Iris-setosa"]),tf.equal(label,["Iris-versicolor"]),tf.equal(label,["Iris-virginica"])])),0))features = tf.transpose(tf.stack([ sepal_length, sepal_width, petal_length, petal_width]))return features, label_number# with tf.Session() as sess:
# coord = tf.train.Coordinator()
# threads = tf.train.start_queue_runners(coord=coord)
# for i in range(1):
# print sess.run(tf.to_int32(tf.stack([
# tf.equal(label,["Iris-setosa"]),
# tf.equal(label,["Iris-versicolor"]),
# tf.equal(label,["Iris-virginica"])
# ])))
# print "-----------------------------------------------------------"
# print sess.run(tf.argmax(tf.to_int32(tf.stack([
# tf.equal(label,["Iris-setosa"]),
# tf.equal(label,["Iris-versicolor"]),
# tf.equal(label,["Iris-virginica"])
# ])),0))
# coord.request_stop()
# coord.join(threads)
def evaluate(sess, X, Y):predicted = tf.cast(tf.argmax(inference(X), 1), tf.int32)print sess.run( tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32)))with tf.Session() as sess:sess.run(tf.global_variables_initializer())X, Y = inputs()coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=coord)tol_loss = loss(X, Y)train_op = train(tol_loss)train_step = 1001for step in range(train_step):sess.run(train_op)if step % 100 == 0:print "%d loss" %step, sess.run(tol_loss)evaluate(sess, X, Y)coord.request_stop()coord.join(threads)
import tensorflow as tf
#对数几率回归参数和变量的初始化
W = tf.Variable(tf.zeros([5, 1]), name="weights")
b = tf.Variable(0.0, name="bias")
#之前的推断现在用于值的合并
def combine_inputs(X):
return tf.matmul(X, W) + b
#新的推断是将sigmoid函数运用到前面的合并
def inference(X):
return tf.sigmoid(combine_inputs(X))
#对于sigmoid函数,标配的损失函数是 交叉熵
def loss(X, Y):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=combine_inputs(X), labels=Y))
#预测与评价模型
def evaluate(sess, X, Y):
predicted = tf.cast(inference(X) > 0.5, tf.float32)
print sess.run(tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32)))
#采用梯度下降优化器
def train(tol_loss):
learning_rate = 0.01
return tf.train.GradientDescentOptimizer(learning_rate).minimize(tol_loss)
#读取csv文件
def read_csv(batch_size, file_name, record_defaults):
filename_queue = tf.train.string_input_producer([file_name])
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)
decoded = tf.decode_csv(value, record_defaults=record_defaults) # 字符串(文本行)转换到指定默认值张量列元组,为每列设置数据类型
return tf.train.shuffle_batch(decoded, batch_size=batch_size, capacity=batch_size * 50,
min_after_dequeue=batch_size) # 读取文件,加载张量batch_size行
def inputs():
passenger_id, survived, pclass, name, sex, age, sibsp, parch, ticket, fare,\
cabin, embarked = read_csv(100, "/home/hadoop/PycharmProjects/tens/train.csv",
[[0.0], [0.0], [0], [""], [""], [0.0], [0.0], [0.0],
[""], [0.0], [""], [""]])
is_first_class = tf.to_float(tf.equal(pclass, [1]))
is_second_class = tf.to_float(tf.equal(pclass, [2]))
is_third_class = tf.to_float(tf.equal(pclass, [3]))
gender = tf.to_float(tf.equal(sex, ["female"]))
features = tf.transpose(tf.stack([is_first_class, is_second_class, is_third_class, gender, age]))
survived = tf.reshape(survived, [100,1])
return features, survived
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
X, Y = inputs()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
tol_loss = loss(X, Y)
train_op = train(tol_loss)
train_step = 1001
for step in range(train_step):
sess.run(train_op)
if step % 100 == 0:
print "%d loss" %step, sess.run(tol_loss)
evaluate(sess, X, Y)
coord.request_stop()
coord.join(threads)
这篇关于sofxmax sigmod的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!