0
我在工作中需要一點幫助。現在,我使用Softmax層作爲神經網絡中分類評分的輸出層。但是,我需要用輸出層上的邏輯層代替Softmax層。我有一些屬於多個類的輸入。 Softmax顯示所有類別的概率,並將類別分配給最高可能性,並很難決定一次預測多於一個類別的閾值。而在邏輯函數的情況下,每個神經元將顯示一個介於(0-1)之間的數字,我可以在這種情況下決定一個閾值。 這裏是我的代碼:如何用Tensorflow中的物流層替換Softmax輸出層?
2層網絡初始化
# Parameters
training_epochs = 10#100
batch_size = 64
display_step = 1
batch = tf.Variable(0, trainable=False)
regualarization = 0.009
# Network Parameters
n_hidden_1 = 250 # 1st layer num features
n_hidden_2 = 250 # 2nd layer num features
n_input = model.layer1_size # Vector input (sentence shape: 30*10)
n_classes = 12 # Sentence Category detection total classes (0-11 categories)
#History storing variables for plots
loss_history = []
train_acc_history = []
val_acc_history = []
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
#Strings
trainingString = "\n\nTraining Accuracy and Confusion Matrix:"
validationString = "\n\nValidation set Accuracy and Confusion Matrix:"
testString = "\n\nTest set Accuracy and Confusion Matrix:"
goldString = "\n\nGold set Accuracy and Confusion Matrix:"
# Create model
def multilayer_perceptron(_X, _weights, _biases):
#Single Layer
#layer_1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1']))
#return tf.matmul(layer_1, weights['out']) + biases['out']
##2 layer
#Hidden layer with RELU activation
layer_1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1']))
#Hidden layer with RELU activation
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2']))
return tf.matmul(layer_2, weights['out']) + biases['out']
# Store layers weight & bias
weights = {
##1 Layer
#'h1': w2v_utils.weight_variable(n_input, n_hidden_1),
#'out': w2v_utils.weight_variable(n_hidden_1, n_classes)
##2 Layer
'h1': w2v_utils.weight_variable(n_input, n_hidden_1),
'h2': w2v_utils.weight_variable(n_hidden_1, n_hidden_2),
'out': w2v_utils.weight_variable(n_hidden_2, n_classes)
}
biases = {
##1 Layer
#'b1': w2v_utils.bias_variable([n_hidden_1]),
#'out': w2v_utils.bias_variable([n_classes])
##2 Layer
'b1': w2v_utils.bias_variable([n_hidden_1]),
'b2': w2v_utils.bias_variable([n_hidden_2]),
'out': w2v_utils.bias_variable([n_classes])
}
# Construct model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
#learning rate
# Optimizer: set up a variable that's incremented once per batch and
# controls the learning rate decay.
learning_rate = tf.train.exponential_decay(
0.02*0.01, # Base learning rate.
batch * batch_size, # Current index into the dataset.
X_train.shape[0], # Decay step.
0.96, # Decay rate.
staircase=True)
#L2 regularization
l2_loss = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables()])
#Softmax loss
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
#Total_cost
cost = cost+ (regualarization*0.5*l2_loss)
# Adam Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost,global_step=batch)
# Initializing the variables
init = tf.initialize_all_variables()
print "Network Initialized!"
我們如何修改每個輸出神經元這個網絡,在(0-1)之間的概率是多少?
我很困惑。所以區別在於你將每個概率發送到sigmoid,然後選擇一個閾值,而不是直接對概率進行閾值處理? – Mai
現在,我得到了softmax概率。但是,我需要S形層,因此每個神經元將從0-1返回一個概率。 Softmax返回所有類別的概率分佈,最高值將被選爲輸入標籤。但是,我有多個標籤,我需要一個門檻來找出這些。在S形的情況下,我會明確設定一個閾值,例如0.5以上所有類都屬於輸入?有意義嗎? –
我的理解是你得到一個分類,你可以從中分類argmax。我看到你想放鬆問題定義,以便獲得多個積極的類。一種方法就是對輸出進行排名並選擇最佳N,如果您不喜歡任意閾值。它也很重要你的分佈如何。如果你的輸出始終給出兩個0.4和其他0.0X(或任何多模式),然後選擇0.2的頂部組或閾值。如果你通過sigmoid傳遞它們,之後你會做同樣的事情,但是他們不需要加上1,這很難解釋。 – Mai