这是从运行脚本以检查Tensorflow是否正常工作时收到的消息:
I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUDA library libcurand.so.8.0 locally
W tensorflow/core/platform/cpu_feature_guard.cc:95] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:95] The TensorFlow library wasn't compiled to use AVX instructions, but these are available …Run Code Online (Sandbox Code Playgroud) 这是来自TensorFlow网站的教程代码,
有谁能帮忙解释一下是什么global_step意思?
我在Tensorflow网站上发现,使用全局步骤计算训练步骤,但我并不完全明白这意味着什么.
此外,数字0在设置时意味着什么global_step?
def training(loss,learning_rate):
tf.summary.scalar('loss',loss)
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
# Why 0 as the first parameter of the global_step tf.Variable?
global_step = tf.Variable(0, name='global_step',trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
Run Code Online (Sandbox Code Playgroud)
根据Tensorflow doc global_step:在变量更新后增加1.这意味着一次更新后global_step变为1吗?
在Tensorflow官方网站上,它给出了如下的解释tf.initialize_all_variables()和tf.global_variables_initializer()功能
tf.initialize_all_variables():
返回初始化所有变量的op.
tf.global_variables_initializer():
添加一个op来初始化模型中的所有变量
似乎两者都可用于初始化图中的所有变量.我们可以交换使用这两个功能吗?如果没有,那会有什么不同?
我试图了解Tensorflow中的NCE损失函数.NCE丢失用于word2vec任务,例如:
# Look up embeddings for inputs.
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
# Construct the variables for the NCE loss
nce_weights = tf.Variable(
tf.truncated_normal([vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)))
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
# Compute the average NCE loss for the batch.
# tf.nce_loss automatically draws a new sample of the negative labels each
# time we evaluate the loss.
loss = tf.reduce_mean(
tf.nn.nce_loss(weights=nce_weights,
biases=nce_biases,
labels=train_labels,
inputs=embed,
num_sampled=num_sampled,
num_classes=vocabulary_size))
Run Code Online (Sandbox Code Playgroud)
更多细节请参考Tensorflow word2vec_basic.py
在word2vec模型中,我们感兴趣的是为单词构建表示.在训练过程中,给定一个滑动窗口,每个单词将有两个嵌入:1)当单词是中心单词时; 2)当单词是上下文单词时.这两个嵌入分别称为输入和输出向量.(输入和输出矩阵的更多解释) …
我可以使用方法参考转换以下代码吗?
List<Text> childrenToRemove = new ArrayList<>();
group.getChildren().stream()
.filter(c -> c instanceof Text)
.forEach(c -> childrenToRemove.add((Text)c));
Run Code Online (Sandbox Code Playgroud)
让我举一个例子来说明我的意思,假设我们有
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(elem -> System.out.println(elem));
Run Code Online (Sandbox Code Playgroud)
使用方法引用它可以写成(最后一行)
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
将表达式转换为方法引用的规则是什么?
已经将word2vec模型读入了genism
model = KeyedVectors.load_word2vec_format('word2vec.50d.txt', binary=False)
Run Code Online (Sandbox Code Playgroud)
似乎genism只提供了从索引到单词的映射,也就是说,model.index2word[2]我们怎样才能在此基础上派生出一个倒置字典(word2index)呢?
为了使案例简单直观,我将使用二进制(0 和 1)分类进行说明。
损失函数
loss = np.multiply(np.log(predY), Y) + np.multiply((1 - Y), np.log(1 - predY)) #cross entropy
cost = -np.sum(loss)/m #num of examples in batch is m
Run Code Online (Sandbox Code Playgroud)
Y 的概率
predY使用 sigmoid 计算,logits可以被认为是到达分类步骤之前神经网络的结果
predY = sigmoid(logits) #binary case
def sigmoid(X):
return 1/(1 + np.exp(-X))
Run Code Online (Sandbox Code Playgroud)
问题
假设我们正在运行一个前馈网络。
输入:[3, 5]:3 是示例的数量,5 是特征大小(捏造的数据)
隐藏单元数:100(只有1个隐藏层)
迭代次数:10000
这种安排被设置为过拟合。当它过拟合时,我们可以完美地预测训练样本的概率;换句话说,sigmoid 输出 1 或 0,确切的数字,因为指数会爆炸。如果是这种情况,我们会有np.log(0)undefined。你通常如何处理这个问题?
这里有人提出这个问题,区别在于我的问题是关注的Estimator.
一些上下文:我们使用估计器训练了一个模型,并在Estimator中定义了一些变量input_fn,该函数将数据预处理到批处理.现在,我们正在进行预测.在预测期间,我们使用相同的input_fn方法读入和处理数据.但得到错误说变量(word_embeddings)不存在(变量存在于chkp图中),这里是相关的代码位input_fn:
with tf.variable_scope('vocabulary', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
word_to_index, word_to_vec = load_embedding(graph_params["word_to_vec"])
word_embeddings = tf.get_variable(initializer=tf.constant(word_to_vec, dtype=tf.float32),
trainable=False,
name="word_to_vec",
dtype=tf.float32)
else:
word_embeddings = tf.get_variable("word_to_vec", dtype=tf.float32)
Run Code Online (Sandbox Code Playgroud)
基本上,当它处于预测模式时,else调用它来加载检查点中的变量.未能识别此变量表示a)范围的不当使用; b)图表未恢复.只要reuse设置得当,我认为范围不重要.
我怀疑这是因为图表尚未恢复input_fn阶段.通常,通过调用saver.restore(sess, "/tmp/model.ckpt") 引用来恢复图形.对估算器源代码的调查并没有得到任何与恢复有关的内容,最好的镜头是MonitoredSession,一个训练的包装器.它已经从最初的问题中伸展出来了,如果我走在正确的道路上,我就没有信心,如果有人有任何见解,我在这里寻求帮助.
我的问题的一行摘要:图表是如何在内部tf.estimator,通过input_fn或恢复的model_fn?
假设我们正在训练神经网络模型来学习从以下输入到输出的映射,其中输出是名称实体(NE).
输入:欧盟拒绝德国呼吁抵制英国羔羊.
产出:ORG O MISC OOO MISC OO
创建滑动窗口以捕获上下文信息,并将其结果作为model_input提供给训练模型.滑动窗口生成如下结果:
[['<s>', '<s>', 'EU', 'rejects', 'German'],\
['<s>', 'EU', 'rejects', 'German', 'call'],\
['EU', 'rejects', 'German', 'call', 'to'],\
['rejects', 'German', 'call', 'to', 'boycott'],\
['German', 'call', 'to', 'boycott', 'British'],\
['call', 'to', 'boycott', 'British', 'lamb'],\
['to', 'boycott', 'British', 'lamb', '.'],\
['boycott', 'British', 'lamb', '.', '</s>'],\
['British', 'lamb', '.', '</s>', '</s>']]
Run Code Online (Sandbox Code Playgroud)
<s>表示句子标记的开始并</s>表示句子结尾标记,并且每个滑动窗口对应于输出中的一个NE.
为了处理这些令牌,使用预训练的嵌入模型将单词转换为向量(例如,手套),但是那些预先训练的模型不包括诸如<s>和之类的标记</s>.我认为随机初始化<s>并</s>不会是一个好主意,因为这种随机结果的规模可能与其他Glove嵌入不一致.
问:用于设置的嵌入有什么建议<s>和</s>为什么?
任务
给定一个 numpy 或 pytorch 矩阵,找到值大于给定阈值的单元格的索引。
我的实现
#abs_cosine is the matrix
#sim_vec is the wanted
sim_vec = []
for m in range(abs_cosine.shape[0]):
for n in range(abs_cosine.shape[1]):
# exclude diagonal cells
if m != n and abs_cosine[m][n] >= threshold:
sim_vec.append((m, n))
Run Code Online (Sandbox Code Playgroud)
顾虑
速度。所有其他计算都建立在 Pytorch 上,使用numpy已经是一种妥协,因为它已经将计算从 GPU 转移到了 CPU。纯 pythonfor循环会使整个过程变得更糟(对于小数据集已经慢了 5 倍)。我想知道我们是否可以在不调用任何for循环的情况下将整个计算移动到 Numpy(或 pytorch)?
我能想到的改进(但卡住了......)
bool_cosine = abs_cosine > 阈值
它返回一个True和的布尔矩阵False。但是我找不到快速检索True单元格索引的方法。
我正在尝试编写一些 python 代码来获得以下输出(所有排列)。region和的值gender是概率,result反映给定事件组合的乘积。
似乎可以通过使用itertoolsand来实现apply,但我不太清楚具体的实现。
输入:
region = {'east': 0.5, 'north': 0.20, 'south': 0.10, 'west': 0.20}
gender = {'female': 0.70, 'male': 0.30}
Run Code Online (Sandbox Code Playgroud)
期望的输出:
result = {('east','female'):0.35,
('east','male'):0.15,
('north','female'):0.14,
('north','male'):0.06,
('south','female'):0.07,
('south','male'):0.03,
('west','female'):0.14,
('west','male'):0.06}
Run Code Online (Sandbox Code Playgroud) 在 NLP 任务中,人们用 SOC(句首)和 EOC(句尾)注释一个句子是很常见的。他们为什么这样做?
它是依赖于任务的性能吗?例如,您在 NER 问题中进行填充的原因与您为翻译问题进行填充的原因不同?就像在 NER 问题中一样,您进行填充以从上下文中提取更多有用的特征,但是在翻译问题中,您进行填充以识别句子的结尾,因为解码器是逐句训练的。
根是Group和3个孩子被添加到组中:来自Rectangle类的正方形,来自类的三角形Polygon和来自类的圆Circle.这三个对象都是用不同的颜色构建的.
任务:当鼠标位于特定对象上时,更改该对象的颜色.如果鼠标不在该对象上,颜色将会改变.
这是我做的:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ColouredShapes extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group group = new Group();
// square
Rectangle square = new Rectangle(40,40);
square.setFill(Color.BLUE);
// triangle
Polygon triangle = new Polygon();
triangle.setLayoutX(80);
triangle.getPoints().addAll(
40.0,0.0,
80.0,40.0,
0.0,40.0
);
triangle.setFill(Color.RED);
//circle
Circle …Run Code Online (Sandbox Code Playgroud) tensorflow ×5
python ×3
java ×2
nlp ×2
numpy ×2
dictionary ×1
gensim ×1
java-8 ×1
java-stream ×1
javafx ×1
pytorch ×1
simd ×1
word2vec ×1
x86 ×1