首页 \ 问答 \ 如何正确使用guava Closer(How to use guava Closer properly)

如何正确使用guava Closer(How to use guava Closer properly)

我正在努力弄清楚如何使用番石榴库中的Closer实用程序。 请参阅下面的代码。

  • 有一点是, IndexWriter在对象初始化和close()上都抛出IOException 。 因此,finally和rethrow块中的代码带有下划线。
  • 另一个问题是,为什么我必须捕获Throwable而不是其他异常类型并且我必须重新抛出错误(我更愿意在现场记录那些)

`

int getDocumentsCount() {
    Closer closer = Closer.create();
    try {
        IndexWriter iwriter = closer.register(openIndexWriter());
        return iwriter.numDocs();
    } catch (Throwable e) {
        logger.error(e, e);
        return -1;
    } finally {
        closer.close();
    }
}


IndexWriter openIndexWriter() throws IOException {
    return new IndexWriter(directory, analyzer, false,
        IndexWriter.MaxFieldLength.UNLIMITED);
}

`

非常感谢

(坚持使用Java 6)


i'm struggling to figure out how to use the Closer utility from the guava library. Please see the code below.

  • One thing is, that the IndexWriter throws an IOException on both object initialization and close(). Therefore, the code in the finally and rethrow blocks is underlined.
  • The other question is, why do i have to catch Throwable instead of other exception types and do i have to rethrow the errors (i would prefer to log those at the spot)

`

int getDocumentsCount() {
    Closer closer = Closer.create();
    try {
        IndexWriter iwriter = closer.register(openIndexWriter());
        return iwriter.numDocs();
    } catch (Throwable e) {
        logger.error(e, e);
        return -1;
    } finally {
        closer.close();
    }
}


IndexWriter openIndexWriter() throws IOException {
    return new IndexWriter(directory, analyzer, false,
        IndexWriter.MaxFieldLength.UNLIMITED);
}

`

Thanks a lot

(stuck with Java 6)


原文:https://stackoverflow.com/questions/39658005
更新时间:2021-12-17 13:12

最满意答案

你可以使用batch_matmul 。 不幸的是,似乎batch_matmul支持批量维度的广播,所以你必须平铺你的w矩阵。 这将使用更多内存,但所有操作都将保留在TensorFlow中

a = tf.ones((5, 2, 3))
b = tf.ones((3, 1))
b = tf.reshape(b, (1, 3, 1))
b = tf.tile(b, [5, 1, 1])
c = tf.batch_matmul(a, b) # use tf.matmul in TF 1.0 
sess = tf.InteractiveSession()
sess.run(tf.shape(c))

这给了

array([5, 2, 1], dtype=int32)

You could use batch_matmul. Unfortunately it doesn't seem batch_matmul supports broadcasting along the batch dimension, so you have to tile your w matrix. This will use more memory, but all operations will stay in TensorFlow

a = tf.ones((5, 2, 3))
b = tf.ones((3, 1))
b = tf.reshape(b, (1, 3, 1))
b = tf.tile(b, [5, 1, 1])
c = tf.batch_matmul(a, b) # use tf.matmul in TF 1.0 
sess = tf.InteractiveSession()
sess.run(tf.shape(c))

This gives

array([5, 2, 1], dtype=int32)

相关问答

更多
  • 使用tf.mul如下: import tensorflow as tf a = tf.constant([[[1,2,1,2],[3,4,1,2],[5,6,10,12]],[[7,8,1,2],[9,10,1,1],[11,12,0,3]]]) b= tf.constant([[[7],[8],[9]],[[1],[2],[3]]]) res=tf.mul(a,b) sess=tf.Session() print(sess.run(res)) 打印: [[[ 7 14 7 14] [ 2 ...
  • 也许你可以研究GSL - GNU科学图书馆的张量扩展 。 Perhaps you could investigate tensor extensions to the GSL — GNU Scientific Library.
  • 尝试 tf.tensordot(A_tf, B_tf,axes = [[1], [0]]) 例如: x=tf.tensordot(A_tf, B_tf,axes = [[1], [0]]) x.get_shape() TensorShape([Dimension(5), Dimension(2), Dimension(3)]) 这是tensordot文档 ,这里是相关的github存储库 。 Try tf.tensordot(A_tf, B_tf,axes = [[1], [0]]) For exam ...
  • 这是一种可能性。 我在第二项中使用了优化,因为它沿着张量的“对角线”放置了c值。 对于第一个项,没有太多优化空间,因为它是一个密集的乘法,所以bsxfun似乎是合适的。 对于第三个学期,我坚持使用bsxfun ,但是由于结果有点稀疏,如果矩阵的大小很大,你可以“手动”填充它。 这是代码: dim = 10; c = [1:dim]'; e = eye(dim); x = zeros([dim, dim, dim]); % initialize with second term x(1:dim*(dim+1 ...
  • 按照公式,你应该只需要将每个坐标乘以相应的质量,将所有的总和除以总质量: import tensorflow as tf # Input volumes volumes = tf.placeholder(tf.float32, [None, 64, 64, 64]) # Make array of coordinates (each row contains three coordinates) ii, jj, kk = tf.meshgrid(tf.range(64), tf.range(64), tf ...
  • 你可以这样做: tf.reduce_sum(tf.expand_dims(v,2)*T,1) 码: m, n, k = 2, 3, 4 T = tf.Variable(tf.random_normal((m,n,k)), name="tensor") v = tf.Variable(tf.random_normal((1,n)), name="vector") c = tf.stack([v,v]) # m times, here set m=2 out1 = tf.matmul(c,T ...
  • 三向张量(或等效的3D阵列或三阶阵列)不一定是秩-3; 这里,“ 张量的等级 ”是指获得原始张量所需的等级-1张量的最小数量(即向量的外积;对于N向张量,它是N向量的外积)。 这在所谓的CP分解的下图中解释。 在上图中,原始张量( x )可以写为R rank-1张量之和,其中R是正整数。 在CP分解中,我们的目标是找到产生原始张量X的最小R. 而这个最小R被称为我们原始张量的等级。 对于3向张量,它是( a1,a2,a3 ... aR; b1,b2,b3 ... bR; c1,c2,c3 ... cR )向 ...
  • 你可以使用tf.gather_nd准确地得到你想要的tf.gather_nd 。 最后的表达是: tf.gather_nd(params, tf.stack([tf.tile(tf.expand_dims(tf.range(tf.shape(indices)[0]), 1), [1, tf.shape(indices)[1]]), tf.transpose(tf.tile(tf.expand_dims(tf.range(tf.shape(indices)[1]), 1), [1, tf.shape(indi ...
  • 除了内存考虑因素,我相信你可以通过一次调用einsum来进行einsum ,尽管你需要一些预处理。 我不完全确定你的意思是“ 因为我收缩了一对指数,结果是一个不属于列表L的新张量 ”,但我认为只需一步就能完成收缩就能解决这个问题。 我建议使用einsum的替代数字索引语法: einsum(op0, sublist0, op1, sublist1, ..., [sublistout]) 所以你需要做的是将索引编码为以整数序列收缩。 首先,您需要首先设置一系列唯一索引,并保留另一个副本以用作子sublisto ...
  • 你可以使用batch_matmul 。 不幸的是,似乎batch_matmul支持批量维度的广播,所以你必须平铺你的w矩阵。 这将使用更多内存,但所有操作都将保留在TensorFlow中 a = tf.ones((5, 2, 3)) b = tf.ones((3, 1)) b = tf.reshape(b, (1, 3, 1)) b = tf.tile(b, [5, 1, 1]) c = tf.batch_matmul(a, b) # use tf.matmul in TF 1.0 sess = tf.I ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)