我需要在Matlab中使用类似hashmap的功能,其中hashmap将向量映射到其他向量,并且事先不知道向量的数量(范围在数十万).
我尝试了Matlab的内置Containers.Map,但是它不接受向量作为键.然后我尝试了java.util.HashMap:
>> map = java.util.HashMap;
>> map.put(1:3,zeros(2,1));
>> map.get(1:3)
ans =
[]
Run Code Online (Sandbox Code Playgroud)
因此,由于某些原因似乎不起作用,即使Java的HashMap应该能够将数组映射到数组.
另一种选择是保留两个单独的矩阵,一个用于键,一个用于值,并逐步增长.但是我不想真的想这样做,因为Matlab中逐渐增长的东西是痛苦的(即使是块大小增量等,例如这里).
问题:1.为什么Java的HashMap不能在这里工作?2.还有其他方法吗?
谢谢.
这段代码
import numpy as np
def some_method(y, threshold):
print type(y), y.shape, y.dtype
c = np.zeros(y.shape)
c[y > threshold] = 1
Run Code Online (Sandbox Code Playgroud)
结果是
<type 'numpy.ndarray'> (484L,) [('target', '<f8')]
DeprecationWarning: using a boolean instead of an integer will result in an error in the future
c[y > threshold] = 1
Run Code Online (Sandbox Code Playgroud)
我在谷歌和numpy发行说明中找不到任何相关内容.我假设这是关于布尔值的索引?我不明白这将如何导致将来出现错误,我该如何解决这个问题?
win32上的Python 2.7.6(默认,2013年11月10日,19:24:24)[MSC v.1500 64位(AMD64)]
Numpy版本:1.8.0
编辑:在代码中添加了print语句,以显示数组是一个ndarray
编辑2:从评论中可以清楚地看到,这y
是因为是一个结构数组,并且检查的正确方法是y['target'] > threshold
.但是,y
有多个列,甚至是不同的列名,有没有办法灵活地使用结构化数组?
编辑:尝试了几件事之后,我在代码中添加了以下内容:
with tf.Session(graph=self.graph) as session:
session.run(tf.initialize_all_variables())
try:
session.run(tf.assert_variables_initialized())
except tf.errors.FailedPreconditionError:
raise RuntimeError("Not all variables initialized!")
Run Code Online (Sandbox Code Playgroud)
现在,偶尔会失败,即tf.assert_variables_initialized()
会tf.initialize_all_variables()
在执行之前立即引发FailedPreconditionError .有谁知道这怎么会发生?
原始问题:
背景
我在通过Tensorflow创建的基本神经网络上使用GradientDescentOptimizer运行交叉验证(CV)超参数搜索.在看似随机的时刻,我得到了一个FailedPreconditionError,用于不同的变量.例如(帖子末尾的完整堆栈跟踪):
FailedPreconditionError: Attempting to use uninitialized value Variable_5
[[Node: Variable_5/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_5"], _device="/job:localhost/replica:0/task:0/gpu:0"](Variable_5)]]
Run Code Online (Sandbox Code Playgroud)
有些跑步失败的速度相当快,有些跑步失败了 - 其中一个跑了15个小时没有问题.我在多个GPU上并行运行 - 不是优化本身,而是每个CV折叠.
我检查了什么
从这个和这篇文章我明白,当尝试使用尚未初始化的变量时会发生此错误tf.initialize_all_variables()
.但是,我99%肯定我正在这样做(如果没有,我希望它总是失败) - 我将在下面发布代码.
该API文档说,
在运行在初始化之前读取tf.Variable的操作时,通常会引发此异常.
"最常见的"表明它也可以在不同的场景中提出.所以,现在主要的问题是:
问题:是否存在可能引发此异常的其他情况,它们是什么?
码
MLP课程:
class MLP(object):
def __init__(self, n_in, hidden_config, n_out, optimizer, f_transfer=tf.nn.tanh, f_loss=mean_squared_error,
f_out=tf.identity, seed=None, global_step=None, graph=None, dropout_keep_ratio=1):
self.graph …
Run Code Online (Sandbox Code Playgroud)