filetypes在Filter(见下图)之间切换,因为它们处于灰色模式,如果设置filetypes如下filetypes = [
("Python File", "*.py"),
("Image File", "*.bmp"),
("All Files", "*.*")
]
Run Code Online (Sandbox Code Playgroud)
.py我们也可以.bmp在窗口中选择,因为test.bmp突出显示。这意味着filetypes、.py和.bmp可以同时激活。这种Filter行为正常吗?我想到的是,我们可以从一组挑选出一个类型filetypes和这些选项应mutually exclusive,也就是说,如果选择Python File (.py)的Filter,那么只有.py文件将可在窗口中选择。
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
# from tkinter.filedialog import askopenfile
# from tkinter.filedialog import askopenfilenames
filetypes = [
("Python File", "*.py"),
("Image …Run Code Online (Sandbox Code Playgroud) 我不知道为什么张量的结果都是 0。这里有什么问题吗?
>>> import torch
>>> import numpy as np
>>> import math
>>> torch.__version__
'0.4.1'
>>> np.__version__
'1.15.4'
>>> torch.arange(0, 10, 2) *-(math.log(10000.0) / 10)
tensor([0, 0, 0, 0, 0])
>>> np.arange(0, 10, 2) *-(math.log(10000.0) / 10)
array([-0. , -1.84206807, -3.68413615, -5.52620422, -7.3682723 ])
>>> torch.arange(0, 10, 2)
tensor([0, 2, 4, 6, 8])
>>> np.arange(0, 10, 2)
array([0, 2, 4, 6, 8])
Run Code Online (Sandbox Code Playgroud) 通常,在构造函数中,我们声明了所有要使用的层。在 forward 函数中,我们定义了模型将如何运行,从输入到输出。
我的问题是,如果直接在函数中调用那些预定义/内置的nn.Modules 会forward()怎样?这个Keras 函数 API风格对于Pytorch是否合法?如果不是,为什么?
更新:以这种方式构建的TestModel确实运行成功,没有报警。但是与传统方式相比,训练损失会缓慢下降。
import torch.nn as nn
from cnn import CNN
class TestModel(nn.Module):
def __init__(self):
super().__init__()
self.num_embeddings = 2020
self.embedding_dim = 51
def forward(self, input):
x = nn.Embedding(self.num_embeddings, self.embedding_dim)(input)
# CNN is a customized class and nn.Module subclassed
# we will ignore the arguments for its instantiation
x = CNN(...)(x)
x = nn.ReLu()(x)
x = nn.Dropout(p=0.2)(x)
return output = x
Run Code Online (Sandbox Code Playgroud) python ×3
pytorch ×2
constructor ×1
file-type ×1
forward ×1
macos ×1
numpy ×1
python-3.x ×1
tkinter ×1