我正在寻找一种处理shell脚本的方法来确定:
它不需要通过依赖项递归,只需列出它直接运行的内容.我本可以写一些自己这样做的东西,但它必须在之前完成......我只是没找到它.
假设我有一个绘图函数,它接受一个axis参数(或返回一个).是否有一些低级方法用于转置整个图,使x轴成为y轴,反之亦然?或者甚至是绘图之前的轴,以便绘图功能通过依赖轴功能正确地完成所有操作(标记)?
我知道如何"手动"执行此操作,但我想知道是否存在允许这种转换的略微隐藏的抽象级别.
假设我有一个生成器,它的__next__()功能有点贵,我想尝试并行化调用。我在哪里投入平行化?
更具体一点,请考虑以下示例:
# fast, splitting a file for example
raw_blocks = (b for b in block_generator(fin))
# slow, reading blocks, checking values ...
parsed_blocks = (block_parser(b) for b in raw_blocks)
# get all parsed blocks into a data structure
data = parsedBlocksToOrderedDict(parsed_blocks)
Run Code Online (Sandbox Code Playgroud)
最基本的事情是将第二行更改为进行并行化的内容。是否有一些生成器魔法可以让一个人并行解压生成器(在第三行)?__next__()并行调用?
我还没弄清楚如何使用pandas DataFrames在python 2和3之间进行pickle加载/保存.在选手中有一个'协议'选项,我玩过但没有成功,但我希望有人有一个快速的想法让我尝试.以下是获取错误的代码:
python2.7
>>> import pandas; from pylab import *
>>> a = pandas.DataFrame(randn(10,10))
>>> a.save('a2')
>>> a = pandas.DataFrame.load('a2')
>>> a = pandas.DataFrame.load('a3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/core/generic.py", line 30, in load
return com.load(path)
File "/usr/local/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/core/common.py", line 1107, in load
return pickle.load(f)
ValueError: unsupported pickle protocol: 3
Run Code Online (Sandbox Code Playgroud)
python3
>>> import pandas; from pylab import *
>>> a = pandas.DataFrame(randn(10,10))
>>> a.save('a3')
>>> a = pandas.DataFrame.load('a3')
>>> a = pandas.DataFrame.load('a2')
Traceback …Run Code Online (Sandbox Code Playgroud) import tensorflow as tf
import numpy as np
x = tf.Variable(2, name='x', trainable=True, dtype=tf.float32)
with tf.GradientTape() as t:
t.watch(x)
log_x = tf.math.log(x)
y = tf.math.square(log_x)
opt = tf.optimizers.Adam(0.5)
# train = opt.minimize(lambda: y, var_list=[x]) # FAILS
@tf.function
def f(x):
log_x = tf.math.log(x)
y = tf.math.square(log_x)
return y
yy = f(x)
train = opt.minimize(lambda: yy, var_list=[x]) # ALSO FAILS
Run Code Online (Sandbox Code Playgroud)
产量值错误:
No gradients provided for any variable: ['x:0'].
Run Code Online (Sandbox Code Playgroud)
这看起来像他们部分给出的例子。我不确定这是一个 Eage 或 2.0 的错误还是我做错了什么。
更新:
由于存在一些问题和有趣的注释,因此粘贴了以下解决方案的修饰版本。
No gradients provided for any variable: ['x:0'].
Run Code Online (Sandbox Code Playgroud) 我收到一个错误:
TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
@tf.function
def has_init_scope():
my_constant = tf.constant(1.)
with tf.init_scope():
added = my_constant * 2
Run Code Online (Sandbox Code Playgroud)
使用如下所示的NVP层:
import tensorflow_probability as tfp
tfb = tfp.bijectors
tfd = tfp.distributions
class NVPLayer(tf.keras.models.Model):
def __init__(self, *, output_dim, num_masked, **kwargs):
super().__init__(**kwargs)
self.output_dim …Run Code Online (Sandbox Code Playgroud) 当脚本由ksh提供时,脚本如何确定它的路径?即
$ ksh ". foo.sh"
Run Code Online (Sandbox Code Playgroud)
我已经看到在BASH上发布的非常好的方法在stackoverflow和其他地方发布但尚未找到ksh方法.
使用"$ 0"不起作用.这只是指"ksh".
更新:我尝试使用"history"命令,但是不知道当前脚本之外的历史记录.
$ cat k.ksh
#!/bin/ksh
. j.ksh
$ cat j.ksh
#!/bin/ksh
a=$(history | tail -1)
echo $a
$ ./k.ksh
270 ./k.ksh
Run Code Online (Sandbox Code Playgroud)
我希望它回显"*./j.ksh".
是否有一个函数来强制索引是唯一的,或者它只能通过转换为dict和back或类似的东西在python'本身'中处理它?
如下面的评论中所述:python pandas是一个基于numpy/scipy构建的项目.
to_dict和返回工作,但我敢打赌,当你获得大奖时,这会变慢.
In [24]: a = pandas.Series([1,2,3], index=[1,1,2])
In [25]: a
Out[25]:
1 1
1 2
2 3
In [26]: a = a.to_dict()
In [27]: a
Out[27]: {1: 2, 2: 3}
In [28]: a = pandas.Series(a)
In [29]: a
Out[29]:
1 2
2 3
Run Code Online (Sandbox Code Playgroud) 我在运行的几个版本的python3上看到pip的以下错误:
...
raise MissingSchema('Proxy URLs must have explicit schemes.')
pip._vendor.requests.exceptions.MissingSchema: Proxy URLs must have explicit schemes.
Run Code Online (Sandbox Code Playgroud)
它看起来像请求库的东西.
这是python 3.3.4上的pip 1.5.2
下面的示例显示了手动清除缓存的简单方法。是否有更标准/稳定的方式来管理未来的缓存?或者也许是一种从一开始就避免这种情况的模式?
在某些情况下,批处理大小变化很大,并且遇到内存问题,因为 def_fun 没有超出范围,并且缓存可能没有清除。
In [164]: @tf.function
...: def f(x):
...: return dict(something=x ** 2)
...:
...:
...:
In [165]: f._list_all_concrete_functions_for_serialization()
Out[165]: []
In [166]: _ = f(tf.convert_to_tensor(np.random.randn(109, 3).astype(np.float32)))
In [167]: _ = f(tf.convert_to_tensor(np.random.randn(111, 3).astype(np.float32)))
In [168]: f._list_all_concrete_functions_for_serialization()
Out[168]:
[<tensorflow.python.eager.function.ConcreteFunction at 0x7fac73e0d358>,
<tensorflow.python.eager.function.ConcreteFunction at 0x7fac71d41a58>]
In [169]: f._stateful_fn._function_cache._garbage_collectors
Out[169]:
[<tensorflow.python.eager.function._FunctionGarbageCollector at 0x7fac94252390>,
<tensorflow.python.eager.function._FunctionGarbageCollector at 0x7fac7b0c6048>,
<tensorflow.python.eager.function._FunctionGarbageCollector at 0x7fac7b0c6d68>]
In [170]: f._stateful_fn._function_cache._garbage_collectors[0]
Out[170]: <tensorflow.python.eager.function._FunctionGarbageCollector at 0x7fac94252390>
In [171]: f._stateful_fn._function_cache._garbage_collectors[0]._cache
Out[171]:
OrderedDict([(CacheKey(input_signature=('UTd1s109-3-u', None), parent_graph=None, device_functions=(), colocation_stack=(), in_cross_replica_context=False),
<tensorflow.python.eager.function.ConcreteFunction at 0x7fac7371def0>),
(CacheKey(input_signature=('UTd1s111-3-u', None), …Run Code Online (Sandbox Code Playgroud)