在下面的TensorFlow函数中,我们必须在最后一层中提供人工神经元的激活.我明白了 但我不明白为什么它被称为logits?这不是一个数学函数吗?
loss_function = tf.nn.softmax_cross_entropy_with_logits(
logits = last_layer,
labels = target_output
)
Run Code Online (Sandbox Code Playgroud) machine-learning neural-network deep-learning tensorflow cross-entropy
在神经网络的输出层中,通常使用softmax函数来近似概率分布:
由于指数,计算起来很昂贵.为什么不简单地执行Z变换以使所有输出都是正的,然后通过将所有输出除以所有输出的总和来归一化?
我知道有很多解释是什么__CODE__
,但我仍然感到困惑.
它只是一种描述损失函数的方法吗?然后,我们可以使用例如梯度下降算法来找到最小值.或者整个过程还包括找到最小算法?
我正在努力在系统范围内或在安装了MariaDB 10的Ubuntu 14.04上的venv中安装mysql-python pip.还尝试使用MariaDB 5.5并获得相同的错误.我没有安装vanilla mysql-server这个问题.
我通过apt-get安装了以下内容:
最初我认为这是一个安装到venv中的问题,但我后来发现mysql-python也不会在全系统安装.下面是我用来安装在venv中的cmds.
virtualenv venv
. venv/bin/activate
pip install mysql-python==1.2.5
In file included from _mysql.c:44:0:
/usr/include/mysql/my_config.h:439:0: warning: "HAVE_WCSCOLL" redefined [enabled by default]
#define HAVE_WCSCOLL
^
In file included from /usr/include/python2.7/pyconfig.h:3:0,
from /usr/include/python2.7/Python.h:8,
from _mysql.c:29:
/usr/include/x86_64-linux-gnu/python2.7/pyconfig.h:911:0: note: this is the location of the previous definition
#define HAVE_WCSCOLL 1
^x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmariadbclient_r -lpthread …
Run Code Online (Sandbox Code Playgroud) 我想知道 TensorFlow 是否有等效的 PyTorch 损失函数softmax_cross_entropy_with_logits
?
我注意到tf.nn.softmax_cross_entropy_with_logits_v2(labels, logits)
主要执行3个操作:
将softmax应用于logits(y_hat)以对其进行标准化:y_hat_softmax = softmax(y_hat)
.
计算交叉熵损失: y_cross = y_true * tf.log(y_hat_softmax)
对实例的不同类求和: -tf.reduce_sum(y_cross, reduction_indices=[1])
从这里借来的代码完美地证明了这一点.
y_true = tf.convert_to_tensor(np.array([[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]]))
y_hat = tf.convert_to_tensor(np.array([[0.5, 1.5, 0.1],[2.2, 1.3, 1.7]]))
# first step
y_hat_softmax = tf.nn.softmax(y_hat)
# second step
y_cross = y_true * tf.log(y_hat_softmax)
# third step
result = - tf.reduce_sum(y_cross, 1)
# use tf.nn.softmax_cross_entropy_with_logits_v2
result_tf = tf.nn.softmax_cross_entropy_with_logits_v2(labels = y_true, logits = y_hat)
with tf.Session() as sess:
sess.run(result)
sess.run(result_tf)
print('y_hat_softmax:\n{0}\n'.format(y_hat_softmax.eval()))
print('y_true: \n{0}\n'.format(y_true.eval()))
print('y_cross: \n{0}\n'.format(y_cross.eval()))
print('result: …
Run Code Online (Sandbox Code Playgroud) TensorFlow将每个输入调用到softmax logit.他们继续将softmax的输入/ logits定义为:"非标定日志概率".
维基百科和其他消息来源说,logit是赔率的对数,是sigmoid/logistic函数的倒数.即,如果sigmoid(x)= p(x),则logit(p(x))= log(p(x)/(1-p(x)))= x.
是否存在TensorFlow调用softmax输入"logits"的数学或常规原因?它们不应该被称为"未缩放的日志概率"吗?
也许TensorFlow只想为二元逻辑回归保留相同的变量名称(使用术语logit是有意义的)和分类逻辑回归...
documentation machine-learning logistic-regression tensorflow softmax
这是我正在使用的代码.我试图得到一个1,0,或者希望得到一个真实测试集的结果概率.当我刚刚分开训练集并在训练集上运行时,我得到了~93%的准确率,但是当我训练程序并在实际测试集上运行时(没有1和0的填充在第1列)它只返回南方的东西.
import tensorflow as tf
import numpy as np
from numpy import genfromtxt
import sklearn
# Convert to one hot
def convertOneHot(data):
y=np.array([int(i[0]) for i in data])
y_onehot=[0]*len(y)
for i,j in enumerate(y):
y_onehot[i]=[0]*(y.max() + 1)
y_onehot[i][j]=1
return (y,y_onehot)
data = genfromtxt('cs-training.csv',delimiter=',') # Training data
test_data = genfromtxt('cs-test-actual.csv',delimiter=',') # Actual test data
#This part is to get rid of the nan's at the start of the actual test data
g = 0
for i in test_data:
i[0] = 1
test_data[g] = …
Run Code Online (Sandbox Code Playgroud) 我在Tensorflow上坚持使用CNN模型.我的代码如下.
# -*- coding: utf-8 -*-
import tensorflow as tf
import time
import json
import numpy as np
import matplotlib.pyplot as plt
import random
import multiprocessing as mp
import glob
import os
Run Code Online (Sandbox Code Playgroud)
def inference(images_placeholder, keep_prob):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# convolution
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# X2 pooling
def max_pool_2x128(x):
return tf.nn.max_pool(x, ksize=[1, 2, 1, 1],strides=[1, 2, 1, 1], …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用预训练的Xception模型和新添加的分类器进行迁移学习。
这是模型:
base_model = keras.applications.Xception(
weights="imagenet",
input_shape=(224,224,3),
include_top=False
)
Run Code Online (Sandbox Code Playgroud)
我使用的数据集oxford_flowers102
直接取自张量流数据集。
这是一个数据集页面。
我在选择某些参数时遇到问题- 要么训练准确性显示可疑的低值,要么存在错误。
我需要帮助指定此参数,对于这个(oxford_flowers102)数据集:
outputs = keras.layers.Dense(102, activation='softmax')(x)
并且我不确定是否应该在此处选择激活功能。我试过:
model.compile(
optimizer=keras.optimizers.Adam(),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.Accuracy()],
)
Run Code Online (Sandbox Code Playgroud)
我不确定它是否应该是SparseCategoricalCrossentropy
或CategoricalCrossentropy
,参数呢from_logits
?
我也不确定是否应该选择指标keras.metrics.Accuracy()
或keras.metrics.CategoricalAccuracy()
我确实缺乏一些理论知识,但现在我只需要这个就能工作。期待您的答复!
machine-learning deep-learning keras tensorflow tensorflow2.0
tensorflow ×7
python ×3
softmax ×3
csv ×1
keras ×1
mariadb ×1
math ×1
mysql-python ×1
pip ×1
pytorch ×1
tensorboard ×1
virtualenv ×1