我试图了解如何使用LSTM对我拥有的某个数据集进行分类.
我研究并发现了这个keras和imdb的例子:https: //github.com/fchollet/keras/blob/master/examples/imdb_lstm.py
但是,我对如何处理数据集进行输入感到困惑.
我知道keras有预处理文本方法,但我不确定使用哪种方法.
x包含带有文本的n行,y通过快乐/悲伤对文本进行分类.基本上,1.0意味着100%快乐,0.0意味着完全悲伤.数字可能会有所不同,例如0.25~~等等.
所以我的问题是,我如何正确输入x和y?我必须用文字袋吗?任何提示表示赞赏!
我在下面编码,但我一直得到同样的错误:
#('Bad input argument to theano function with name ... at index 1(0-based)',
'could not convert string to float: negative')
Run Code Online (Sandbox Code Playgroud)
import keras.preprocessing.text
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
print('Loading data...')
import pandas
thedata = pandas.read_csv("dataset/text.csv", sep=', ', delimiter=',', header='infer', names=None)
x = thedata['text']
y = thedata['sentiment']
x = …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问以主目录子目录为根的文件。为此,我正在使用os.walk(). 我能够成功访问文件名并将其存储在列表中。但是,当我尝试使用 . 打开这些文件时open(filename, 'r'),我收到一条错误消息,告诉我没有这样的文件或目录存在。
我认为这里的问题是我只访问'names'而不是实际文件本身。我的代码如下所示:
list_of_files = {}
for (dirpath, dirnames, filenames) in
os.walk("C:\\Users\\Akarshkb\\Desktop\\cs361hw\\HMP_Dataset"):
for filename in filenames:
if filename.endswith('.txt'):
list_of_files[filename] = os.sep.join([dirpath, filename])
file = open(filename, 'r')
file.read()
file.close()
print (list_of_files)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
`FileNotFoundError Traceback (most recent call
last)
<ipython-input-40-10ae3e92446a> in <module>()
4 if filename.endswith('.txt'):
5 list_of_files[filename] = os.sep.join([dirpath,
filename])
----> 6 file = open(filename, 'r')
7 file.read()
8 file.close()
FileNotFoundError: [Errno 2] No such file or directory: 'Accelerometer-2011-
04-11-13-28-18-brush_teeth-f1.txt'`
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
我正在尝试使用 PyTorch执行名为“训练分类器”的教程。当尝试调试这部分代码时:
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
Files already downloaded and verified Files already downloaded and verified
Files already downloaded and verified …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过安装库(allennlp)pip3。但它抱怨PyTorch版本。虽然allennlp需要torch=0.4.0我有torch=0.4.1:
...
Collecting torch==0.4.0 (from allennlp)
Could not find a version that satisfies the requirement torch==0.4.0 (from allennlp) (from versions: 0.1.2, 0.1.2.post1, 0.4.1)
No matching distribution found for torch==0.4.0 (from allennlp)
Run Code Online (Sandbox Code Playgroud)
也可以手动安装:
pip3 install torch==0.4.0
Run Code Online (Sandbox Code Playgroud)
也不起作用:
Could not find a version that satisfies the requirement torch==0.4.0 (from versions: 0.1.2, 0.1.2.post1, 0.4.1)
No matching distribution found for torch==0.4.0
Run Code Online (Sandbox Code Playgroud)
其他版本也一样。
Python是在Mac OS上Python 3.7.0通过安装的版本brew。
我记得前段时间我可以在版本0.4.0和0.3.1使用之间进行切换 …
我试图在不使用 Pytorch 的 autograd 的情况下计算 1/x 的梯度。我使用公式 grad(1/x, x) = -1/x**2。当我将这个公式的结果与 Pytorch 的 autograd 给出的梯度进行比较时,它们是不同的。
这是我的代码:
a = torch.tensor(np.random.randn(), dtype=dtype, requires_grad=True)
loss = 1/a
loss.backward()
print(a.grad - (-1/(a**2)))
Run Code Online (Sandbox Code Playgroud)
输出是:
tensor(5.9605e-08, grad_fn=<ThAddBackward>)
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释问题是什么?
我收到此错误:
Exception in Thread: ValueError: signal number 32 out of range
Run Code Online (Sandbox Code Playgroud)
给我带来问题的具体教程是训练分类器(https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html),具体行是:
dataiter = iter(trainloader)
Run Code Online (Sandbox Code Playgroud)
完整的错误回溯是:
Exception in thread Thread-5:
Traceback (most recent call last):
File "/home/chenchen/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/home/chenchen/anaconda3/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/chenchen/anaconda3/lib/python3.6/multiprocessing/resource_sharer.py", line 139, in _serve
signal.pthread_sigmask(signal.SIG_BLOCK, range(1, signal.NSIG))
File "/home/chenchen/anaconda3/lib/python3.6/signal.py", line 60, in pthread_sigmask
sigs_set = _signal.pthread_sigmask(how, mask)
ValueError: signal number 32 out of range
Run Code Online (Sandbox Code Playgroud)
我的操作系统是 Ubuntu 18.10,我的 python 环境是 python 3.6 的 Anaconda3。我从最新源安装了 pytorch。我的 cuda 版本是 …
昨天我遇到了这个问题,第一次注意到线性层的权重nn.Linear需要在应用之前进行转置matmul。
应用权重的代码:
output = input.matmul(weight.t())
Run Code Online (Sandbox Code Playgroud)
这是什么原因呢?
为什么权重不是从一开始就处于转置形状,因此不需要每次在应用图层之前都进行转置?
matrix matrix-multiplication neural-network deep-learning pytorch
我不知道为什么张量的结果都是 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) 我正在寻找一种好的(有效的,最好是简单的)方法来从可变长度/形状的序列创建填充张量.到目前为止,我能想象的最好的方法是这样一个天真的方法:
import torch
seq = [1,2,3] # seq of variable length
max_len = 5 # maximum length of seq
t = torch.zeros(5) # padding value
for i, e in enumerate(seq):
t[i] = e
print(t)
Run Code Online (Sandbox Code Playgroud)
输出:
tensor([ 1., 2., 3., 0., 0.])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
我还没有找到一些东西,但我想一定有更好的东西.
我正在考虑一些函数,用所需的填充将序列张量扩展到所需的形状.或者直接从序列中创建填充张量的东西.但当然也欢迎其他方法.
如果没有,是否有项目在 Flask-Login 中添加了此功能?否则,从 Flask-Login 迁移到 Flask-User 似乎有点令人生畏。否则,是否有从 Flask-Login 迁移到 Flask-User 的任何方向?