Tensorflow的逆置换操作函数tf.invert_permutation

tf.invert_permutation(x,name=None)

计算序列的逆置换(inverse permutation)。

本操作是计算张量的索引的逆置换。x是一维的整数张量,表示一个以0为开始的索引数组,然后根据这里元素的值来放入整数什,计算公式如下:

y[x[i]] = i for i in [0, 1, ..., len(x) -1]

从0开始,没有重复和负数值。

例如:

# 张量 `x` 是 [3, 4, 0, 2, 1]

invert_permutation(x) ==> [2, 4, 3, 0, 1]

参数:

·        x: 张量,必须是下面类型之一: int32, int64。 1-D。

·        name: 操作的名称,可选。

返回值:

张量,与x类型相同。1-D。

#python 3.5.3/TensorFlow 1.0.1/win 10    
#2017-04-26 蔡军生  http://blog.csdn.net/caimouse    
#
#导入要使用的库
import tensorflow as tf
import numpy as np

#演示API的类
class DemoTF:
    def __init__(self):
        '''构造函数'''
        self.num1 = tf.Variable([1, 2, 0, 3], dtype = tf.int32)
        self.num2 = tf.Variable([1, 3, 0, 2], dtype = tf.int32) 
        self.invert1 = tf.invert_permutation(self.num1)
        self.invert2 = tf.invert_permutation(self.num2)  

    def run_graph(self):       
        init = tf.global_variables_initializer()  # 初始化变量
        with tf.Session() as sess:            
            sess.run(init)
            self.main(sess) #运行主函数
    def main(self, sess):
        '''主函数'''        
        print(sess.run(self.num1))         
        print(sess.run(self.invert1))

        print('num2: \n', sess.run(self.num2))         
        print(sess.run(self.invert2))
                
#主程序入口
if __name__ == "__main__":
    demo = DemoTF()
    demo.run_graph()

结果输出如下:

[1 2 0 3]
[2 0 1 3]
num2: 
 [1 3 0 2]
[2 0 3 1]
>>>


1. TensorFlow API攻略

2. TensorFlow入门基本教程

3. C++标准模板库从入门到精通 

4.跟老菜鸟学C++

5. 跟老菜鸟学python

6. 在VC2015里学会使用tinyxml库

7. 在Windows下SVN的版本管理与实战 

 http://edu.csdn.net/course/detail/2579

8.Visual Studio 2015开发C++程序的基本使用 

http://edu.csdn.net/course/detail/2570

9.在VC2015里使用protobuf协议

10.在VC2015里学会使用MySQL数据库



为将代码改为八层介质,需要将代码中的两层介质部分进行修改。修改后的代码如下: ```python import numpy as np import matplotlib.pyplot as plt def ricker(f, length, dt): t = np.arange(-length/2,(length-dt)/2, dt) y = (1.0 - 2.0*(np.pi*f*t)**2) * np.exp(-(np.pi*f*t)**2) return t,y i = 0 Frequency = 20 length = 0.128 dt = 0.001 # 八层介质 rho = np.array([1.5, 1.8, 2.2, 2.4, 2.6, 2.8, 3.0, 3.2]) v = np.array([1500, 1700, 2000, 2200, 2400, 2600, 2800, 3000]) depth = np.array([0, 50, 100, 150, 200, 250, 300, 350]) Z = rho * v L = (Z[1:] - Z[:-1]) / (Z[1:] + Z[:-1]) t1 = np.arange(0, depth[-1]/v[0]*2, dt) L1 = np.zeros(np.size(t1)) for i in range(1, np.size(depth)): t = depth[i]/v[i-1] + depth[i]/v[i] L1[int(np.round(t/dt))] = L[i-1] t0, w0 = ricker(Frequency, length, dt) syn = np.convolve(L1, w0, 'same') fig = plt.figure(num=1, figsize=(20,15),dpi=300) ax1 = fig.add_subplot(1, 3 , 1) ax1.plot(w0, t0) ax1.xaxis.set_ticks_position('top') ax1.invert_yaxis() ax1.set_title("Amplitude", fontsize = 12) ax1.set_ylabel("Time(s)",fontsize = 12) ax2 = fig.add_subplot(1, 3, 2) ax2.plot(L1, t1) ax2.xaxis.set_ticks_position('top') ax2.invert_yaxis() ax2.set_title("Reflection coefficient", fontsize = 12) ax2.set_ylabel("Two-way travel time(s)",fontsize = 12) ax3 = fig.add_subplot(1, 3, 3) ax3.plot(syn, t1) ax3.xaxis.set_ticks_position('top') ax3.invert_yaxis() ax3.set_title("Amplitude", fontsize = 12) ax3.set_ylabel("Two-way travel time(s)",fontsize = 12) fig.suptitle('Eight-layer synthetic seismogram', fontsize = 18) plt.tight_layout() ``` 修改后的代码中,我们将八层介质的密度和速度分别存储在 `rho` 和 `v` 数组中,深度存储在 `depth` 数组中。通过计算每一层的阻抗 `Z` 和反射系数 `L`,并在 `for` 循环中计算每一时刻的反射系数 `L1`。最后计算合成地震记录 `syn`,并绘制三个子图,其中第一个子图为初始波形,第二个子图为反射系数,第三个子图为合成地震记录。总体来说,代码中的计算过程和原始代码是类似的,只是修改了介质的层数和属性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值