用于tensorflow的高级张量网络api。

tensornetwork的Python项目详细描述


t网络

Build Status

tensorflow、jax、pytorch和numpy的tensor网络包装器。

有关张量网络的概述,请参见以下内容:

更多信息可在我们的TensorNetwork文件中找到:

复制这些论文结果的代码可以在experiments目录中找到。

安装

pip3 install tensornetwork

安装在Docker上

这将创建包含TensorNetwork的Docker图像。它将TensorNetwork安装与系统的其他部分隔离。

  1. Install Docker在主机系统上。

  2. 为系统生成Docker映像:

git clone https://github.com/google/TensorNetwork
cd TensorNetwork
docker build -t google/tensornetwork . # This builds the actual image based on latest Ubuntu, and installs TensorNetwork with the needed dependencies.

安装在Docker上进行TensorNetwork开发

要在Docker虚拟机中进行TensorNetwork开发,可以使用dev_tools/Dockerfile:

git clone https://github.com/google/TensorNetwork
cd TensorNetwork/dev_tools
docker build -t google/tensornetwork-dev . # This builds the actual image based on latest Ubuntu, cloning the TensorNetwork tree into it with the needed dependencies.
docker run -it google/tensornetwork-dev

如果您想对tensornetwork进行更改,那么您将改为分叉存储库并从分叉提交pull请求。

文档

有关tensornetwork api的详细信息,请参见reference documentation.

基本示例

注意:下面的示例假设tensorflow v2接口 (在tf 1.13或更高版本中,在 导入tensorflow),但也应该使用eager模式 (tf.enable_eager_execution())。实际的图书馆确实有用 在图形模式下,但文档有限。

在这里,我们构建一个简单的2节点压缩。

importnumpyasnpimporttensorflowastftf.enable_v2_behavior()importtensornetwork# Create the networknet=tensornetwork.TensorNetwork()# Add the nodesa=net.add_node(np.ones((10,),dtype=np.float32))# Can use either np.array or tf.Tensor and can even mix them!b=net.add_node(tf.ones((10,)))edge=net.connect(a[0],b[0])final_node=net.contract(edge)print(final_node.tensor.numpy())# Should print 10.0

优化收缩。

通常,为了避免跟踪边,在收缩平行边之前将其展平在计算上更有效。

net=tensornetwork.TensorNetwork()a=net.add_node(tf.ones((2,2,2)))b=net.add_node(tf.ones((2,2,2)))e1=net.connect(a[0],b[0])# Edge contraction is communative, so the order doesn't matter.e2=net.connect(b[1],a[1])e3=net.connect(a[2],b[2])flattened_edge=net.flatten_edges([e1,e2,e3])print(net.contract(flattened_edge).tensor.numpy())

为了您的方便,我们还有contract_betweencontract_parallel

# Contract all of the edges between a and b.net.contract_between(a,b)# Contract all of edges that are parallel to edge # (parallel means connected to the same nodes).net.contract_parallel(edge)

拆分节点

可以通过执行奇异值分解来拆分节点。

# This will return two nodes and a tensor of the truncation error.# The two nodes are the unitary matricies multiplied by the square root of the# singular values.# The `left_edges` are the edges that will end up on the `u_s` node, and `right_edges`# will be on the `vh_s` node.u_s,vh_s,trun_error=net.split_node(node,left_edges,right_edges)# If you want the singular values in it's own node, you can use `split_node_full_svd`.u,s,vh,trun_error=net.split_node_full_svd(node,left_edges,right_edges)

节点和边缘名称。

可以选择命名节点/边。这对调试很有用, 因为所有错误消息都将打印断开边/节点的名称。

net=tensornetwork.TensorNetwork()node=net.add_node(np.eye(2),name="Identity Matrix")print("Name of node: {}".format(node.name))edge=net.connect(node[0],node[1],name="Trace Edge")print("Name of the edge: {}".format(edge.name))# Adding name to a contraction will add the name to the new edge created.final_result=net.contract(edge,name="Trace Of Identity")print("Name of new node after contraction: {}".format(final_result.name))

命名轴。

<>为了记住轴更容易做什么,你可以选择命名一个节点的轴。

net=tensornetwork.TensorNetwork()a=net.add_node(np.zeros((2,2)),axis_names=["alpha","beta"])edge=net.connect(a["beta"],a["alpha"])

边缘重新排序。

若要断言结果的轴的顺序正确,可以在计算期间随时对节点重新排序。

net=tensornetwork.TensorNetwork()a=net.add_node(np.zeros((1,2,3)))e1=a[0]e2=a[1]e3=a[2]a.reorder_edges([e3,e1,e2])# If you already know the axis values, you can equivalently do# a.reorder_axes([2, 0, 1])print(a.tensor.shape)# Should print (3, 1, 2)

NCON接口。

对于张量网络及其压缩的更紧凑的规范,有ncon()。例如:

fromtensornetworkimportncona=tf.random_normal((2,2))b=tf.random_normal((2,2))c=ncon([a,b],[(-1,1),(1,-2)])print(tf.norm(tf.matmul(a,b)-c))# Should be zero

也可以生成TensorNetwork

fromtensornetworkimportncon_networka=tf.random_normal((2,2))b=tf.random_normal((2,2))net,e_con,e_out=ncon_network([a,b],[(-1,1),(1,-2)])foreine_con:n=net.contract(e)# Contract edges in ordern.reorder_edges(e_out)# Permute final tensor as necessaryprint(tf.norm(tf.matmul(a,b)-n.tensor))

不同的后端支持。

目前,我们支持tensorflow、jax和numpy作为tensornetwork后端。

要更改默认全局后端,可以执行以下操作:

tensornetwork.set_default_backend("jax")# numpy, tensorflow, pytorch

或者,如果您只想更改单个TensorNetwork的后端,则可以执行以下操作:

tensornetwork.TensorNetwork(backend="jax")

高级示例

一些更复杂的例子可以在examples/下找到。

波函数的trotter演化

演示通过应用量子电路实现的波函数的时间演化 由传播子的快步分解而来。要从源代码运行,请使用

python -m examples.wavefunctions.evolution_example

从根目录。

免责声明

这个库位于alpha中,将经历许多突破性的变化。虽然发行版对于研究来说足够稳定,但我们不建议在任何生产环境中使用它。

TensorNetwork不是谷歌的官方产品。版权所有2019 TensorNetwork开发者。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
面向批处理的java非阻塞队列   java如何基于HttpComponent获取html内容   java为什么我的程序会显示此错误?   java spring控制器如何处理应用程序/octetstream请求?   尝试将数据插入数据库时出现java常规错误   无法在java多线程处理中维护生产者任务的顺序   java为什么JSON数据无法访问ViewPager?   java获取Maven中特定分支的SVN buildnumber   java客户端无法从服务器接收信息   java等轴测地图绘制,生成   java无法调试ProcessBuilder   java热点JVM阵列分配   在数组中存储“inputdialog”数据的java   saml Java Inflater引发DataFormatException“无效代码长度集”   从集合(爬虫、Jsoup、Java)写入文件