我使用张量流来实现一个简单的多层感知器用于回归.代码是从标准的mnist分类器修改的,我只将输出成本更改为MSE(使用tf.reduce_mean(tf.square(pred-y))),以及一些输入,输出大小设置.但是,如果我使用回归训练网络,在几个时期之后,输出批次完全相同.例如:
target: 48.129, estimated: 42.634
target: 46.590, estimated: 42.634
target: 34.209, estimated: 42.634
target: 69.677, estimated: 42.634
......
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的批量大小,不同的初始化,使用sklearn.preprocessing.scale进行输入规范化(我的输入范围非常不同).但是,它们都没有奏效.我还尝试了Tensorflow的一个sklearn示例(波士顿数据的深度神经网络回归).但我在第40行得到了另一个错误:
'module'对象没有属性'infer_real_valued_columns_from_input'
任何人都有关于问题所在的线索?谢谢
我的代码如下所示,可能有点长,但非常简单:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import learn
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn import datasets, linear_model
from sklearn import cross_validation
import numpy as np
boston = learn.datasets.load_dataset('boston')
x, y = boston.data, boston.target
X_train, X_test, Y_train, Y_test = …Run Code Online (Sandbox Code Playgroud) 我试图在张量流中训练一个稀疏变量,据我所知,当前张量流不允许稀疏变量.
我找到了两个讨论类似问题的线程:using-sparsetensor-as-a-trainable-variable和update-only-the-the-word-embedding-matrix-in-tensorflow.我不太明白答案,如果有任何示例代码会很好
我试过的一种方法是:
# initialize the sparse variable sp_weights
# assuming w_s is the input sparse matrix contains indices information
dim=20
identity = tf.constant(np.identity(dim), dtype=tf.float32)
A=tf.sparse_tensor_dense_matmul(w_s, identity) # convert w_s to dense
w_init = tf.random_normal([dim, dim], mean=0.0, stddev=0.1)
w_tensor = tf.mul(A, w_init) # random initialize sparse tensor
vars['sp_weights'] = tf.Variable(w_tensor)
# doing some operations...
Run Code Online (Sandbox Code Playgroud)
当计算梯度时,根据第二个链接使用tf.IndexedSlices
grad = opt.compute_gradients(loss)
train_op = opt.apply_gradients(
[tf.IndexedSlices(grad, indices)]) # indices is extracted from w_s
Run Code Online (Sandbox Code Playgroud)
上面的代码当然不起作用,我在这里很困惑.tf.IndexedSlices使输入成为IndexedSlices实例,如何使用它来更新给定索引的渐变?此外,许多人提到使用tf.scatter_add/sub/update.官方文档不包含有关如何使用以及在何处使用渐变更新的示例代码.我应该使用tf.IndexedSlices还是tf.scatter?如果有任何示例代码,将会非常有用.谢谢!