文档提供了很好的示例,如何提供元数据.但是,当我为数据帧选择正确的dtypes时,我仍然感到不确定.
meta={'x': int 'y': float,
'z': float}
代替meta={'x': 'i8', 'y': 'f8', 'z': 'f8'}
吗?运行以下代码时,dask.dataframe.head()的结果取决于npartitions:
import dask.dataframe as dd
import pandas as pd
df = pd.DataFrame({'A': [1,2,3], 'B': [2,3,4]})
ddf = dd.from_pandas(df, npartitions = 3)
print(ddf.head())
Run Code Online (Sandbox Code Playgroud)
这产生以下结果:
A B
0 1 2
Run Code Online (Sandbox Code Playgroud)
但是,当我将npartitions设置为1或2时,我得到了预期的结果:
A B
0 1 2
1 2 3
2 3 4
Run Code Online (Sandbox Code Playgroud)
似乎重要的是,npartition低于数据帧的长度.这是有意的吗?在将数据转换为dask框架之前,我是否总是必须检查数据的大小?
是否有可能迭代一个dask GroupBy对象来访问底层数据帧?我试过了:
import dask.dataframe as dd
import pandas as pd
pdf = pd.DataFrame({'A':[1,2,3,4,5], 'B':['1','1','a','a','a']})
ddf = dd.from_pandas(pdf, npartitions = 3)
groups = ddf.groupby('B')
for name, df in groups:
print(name)
Run Code Online (Sandbox Code Playgroud)
但是,这会导致错误: KeyError: 'Column not found: 0'
更一般地说,除了apply方法之外,dask GroupBy对象允许哪种交互?
比方说,我有以下的dask数据帧.
dict_ = {'A':[1,2,3,4,5,6,7], 'B':[2,3,4,5,6,7,8], 'index':['x1', 'a2', 'x3', 'c4', 'x5', 'y6', 'x7']}
pdf = pd.DataFrame(dict_)
pdf = pdf.set_index('index')
ddf = dask.dataframe.from_pandas(pdf, npartitions = 2)
Run Code Online (Sandbox Code Playgroud)
此外,我有一个我感兴趣的指数列表,例如
indices_i_want_to_select = ['x1','x3', 'y6']
Run Code Online (Sandbox Code Playgroud)
如何生成一个新的dask数据帧,它只包含索引指定的行?有没有理由,为什么有些像ddf [ddf.A> = 4]是可能的,而ddf [indices_i_want_to_select中的ddf.index]或ddf.loc [indices_i_want_to_select]不是?
dask API 说, map_partition 可用于“在每个 DataFrame 分区上应用 Python 函数”。根据这个描述和“map”的通常行为,我希望 map_partitions 的返回值是(类似于)一个长度等于分区数的列表。列表的每个元素都应该是函数调用的返回值之一。
但是,对于以下代码,我不确定返回值取决于什么:
#generate example dataframe
pdf = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
ddf = dd.from_pandas(pdf, npartitions=3)
#define helper function for map. VAL is the return value
VAL = pd.Series({'A': 1})
#VAL = pd.DataFrame({'A': [1]}) #other return values used in this example
#VAL = None
#VAL = 1
def helper(x):
print('function called\n')
return VAL
#check result
out = ddf.map_partitions(helper).compute()
print(len(out))
Run Code Online (Sandbox Code Playgroud)
VAL = pd.Series({'A': 1})
导致 4 个函数调用(可能一个用于推断 dtype,3 个用于推断分区)和一个 len == 3 和 …是否可以使用cupy
(或chainer
)从GPU异步传输内存到GPU ?
我正在训练一个相对较小的网络,其中包含无法容纳到GPU内存中的非常大的数据。此数据应保存在CPU内存中,并依次提供给GPU进行小批量计算。
内存传输时间是此应用程序的主要瓶颈。我认为异步内存传输解决了这个问题,即在计算一个小批量时,另一个小批量会在后台传输到GPU。
我想知道cupy.cuda.Stream
上课有可能吗,但我还不知道。我将不胜感激任何意见/建议。
编辑:我认为以下代码使异步内存传输,但不是。
import numpy as np
import cupy as cp
a_cpu = np.ones((10000, 10000), dtype=np.float32)
b_cpu = np.ones((10000, 10000), dtype=np.float32)
a_stream = cp.cuda.Stream(non_blocking=True)
b_stream = cp.cuda.Stream(non_blocking=True)
a_gpu = cp.empty_like(a_cpu)
b_gpu = cp.empty_like(b_cpu)
a_gpu.set(a_cpu, stream=a_stream)
b_gpu.set(b_cpu, stream=b_stream)
# This should start before b_gpu.set() is finished.
a_gpu *= 2
Run Code Online (Sandbox Code Playgroud)
nvvp显示内存转移是顺序发生的。
我有一个类似数据库的对象,其中包含许多 dask 数据帧。我想处理数据,保存并在第二天重新加载以继续分析。
因此,我尝试使用pickle保存dask数据帧(不是计算结果,只是“计算计划”本身)。显然,它是有效的(至少,如果我在同一台机器上解开对象)......但是有一些陷阱吗?
我收到错误
Dask - 警告 - Worker 超出了 95% 的内存预算。
我正在使用具有 4 个物理内核和 8 个虚拟内核的本地 PC,我尝试了以下操作:
每...
...以及此处的文档...
https://distributed.readthedocs.io/en/latest/worker.html#memory-management
...我尝试编辑 .config\dask\distributed.yaml 以取消对底部五行的注释...
distributed:
worker:
# Fractions of worker memory at which we take action to avoid memory blowup
# Set any of the lower three values to False to turn off the behavior entirely
memory:
target: 0.60 # target fraction to stay below
spill: 0.70 # fraction at which we spill to disk
pause: 0.80 # fraction …
Run Code Online (Sandbox Code Playgroud) matplotlib 中的动画模块通常需要第三方模块,如 FFmpeg、mencoder 或 imagemagik 才能将动画保存到文件中(例如:https : //stackoverflow.com/a/25143651/5082048)。
甚至 matplotlib 中的 MovieWriter 类似乎也是以将第三方模块合并的方式构建的(启动和关闭进程,通过管道进行通信):http ://matplotlib.org/api/animation_api.html#matplotlib.animation.MovieWriter .
我正在寻找一种方法,如何将matplotlib.animation.FuncAnimation
对象框架保存到框架到 png - 直接在 python 中。之后,我想使用这种方法在 iPython 笔记本中将 .png 文件显示为动画:https : //github.com/PBrockmann/ipython_animation_javascript_tool/
因此我的问题是:
matplotlib.animation.FuncAnimation
对象直接保存到 .png 文件而无需使用第三方模块?编辑:matplotlib.animation.FuncAnimation
给出了对象,任务是使用纯 Python 保存它的帧。不幸的是,我无法像 ImportanceOfBeingErnest 建议的那样更改底层动画功能。
我正在尝试进行非常简单的学习,以便更好地理解 PyTorch 和 LSTM 的工作原理。为此,我试图学习从输入张量到两倍值的输出张量(相同形状)的映射。因此[1 2 3]
,作为输入应该学习[2 4 6]
作为输出。为此,我有一个dataloader
:
class AudioDataset(Dataset):
def __init__(self, corrupted_path, train_set=False, test_set=False):
torch.manual_seed(0)
numpy.random.seed(0)
def __len__(self):
return len(self.file_paths)
def __getitem__(self, index):
random_tensor = torch.rand(1, 5) * 2
random_tensor = random_tensor - 1
return random_tensor, random_tensor * 2
Run Code Online (Sandbox Code Playgroud)
我的 LSTM 本身非常简单:
class MyLSTM(nn.Module):
def __init__(self, input_size=4000):
super(MyLSTM, self).__init__()
self.lstm = nn.LSTM(input_size=input_size, hidden_size=input_size,
num_layers=2)
def forward(self, x):
y = self.lstm(x)
return y
Run Code Online (Sandbox Code Playgroud)
我的训练看起来像:
train_loader = torch.utils.data.DataLoader(
train_set, batch_size=1, shuffle=True, **kwargs)
model = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的脚本,该脚本将给出 1 到 100 之间的随机数,并根据结果打印出“你赢了”或“你输了”。当测试特定数字时,== 1
它工作正常,但是当用它替换它时,<=
会出现以下错误:TypeError: '<' not supported between instances of 'NoneType' and 'int'
这是我的代码:
import random
number = print(random.randint(1, 100))
if number <= 20:
print("you win")
else:
print("you lose")
Run Code Online (Sandbox Code Playgroud)