tensorboardX add_graph()ImportError: cannot import name 'OperatorExportTypes' - tensorboardx

When i use the tensorbosrdX :
import torch
import torch.nn as nn
import torch.nn.functional as F
from tensorboardX import SummaryWriter
class Net1(nn.Module):
def __init__(self):
super(Net1, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
self.bn = nn.BatchNorm2d(20)
def forward(self, x):
x = F.max_pool2d(self.conv1(x), 2)
x = F.relu(x) + F.relu(-x)
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = self.bn(x)
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
x = F.softmax(x, dim=1)
return x
dummy_input = torch.rand(13, 1, 28, 28)
model = Net1()
with SummaryWriter(comment='Net1') as w:
w.add_graph(model, (dummy_input,))
`
I get this error:
Traceback (most recent call last):
File "/home/user/pych/tensorboardtest.py", line 34, in <module>
w.add_graph(model, (dummy_input,))
File "/home/user/anaconda3/envs/jointpy35/lib/python3.5/site-packages/tensorboardX/writer.py", line 566, in add_graph
self.file_writer.add_graph(graph(model, input_to_model, verbose))
File "/home/user/anaconda3/envs/jointpy35/lib/python3.5/site-packages/tensorboardX/pytorch_graph.py", line 171, in graph
from torch.onnx.utils import OperatorExportTypes
ImportError: cannot import name 'OperatorExportTypes'
Process finished with exit code 1
And this code is an example of how to use add_graph().But it cant work in my computer and i dont know how to solve it
My environment:
torch : 0.4.0
python:3.5.0
tensorboardX 1.6

you need update the version of pytorch to 1.0.1

Related

I want to draw big network graph with python networkx

I have network with text file.
This is network file format.
(source,target,weight)
(2,167480,2)
(2,257789,1)
(2,1838,1)
The network file have 403915 nodes(source).
This is my code
# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
f = open("C:\\Users\\UrbanLab-4\\Desktop\\exercise\\remake3.txt",'r')
f_1 = f.read().split()
i = []
G = nx.Graph()
for lineF in f_1:
i = lineF.split(',',2)
G.add_nodes_from(i)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos, cmap=plt, node_color = 'values')
nx.draw_networkx_edges(G,pos,arrows=True)
plt.show()
Then I have this error..
Traceback (most recent call last):
File "C:/Users/UrbanLab-4/PycharmProjects/untitled/net.py", line 12, in <module>
pos = nx.spring_layout(G)
File "C:\Python27\lib\site-packages\networkx\drawing\layout.py", line 287, in fruchterman_reingold_layout
A = nx.to_numpy_matrix(G, weight=weight)
File "C:\Python27\lib\site-packages\networkx\convert_matrix.py", line 369, in to_numpy_matrix
M = np.zeros((nlen,nlen), dtype=dtype, order=order) + np.nan
MemoryError
How solve this problem?

How I can invoke importing class in other class Python

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import getopt
import alsaaudio
import numpy
from time import sleep
class A_weight():
def __init__(self):
skaler = 2.361E-14
fix_cur = 0.20565360419770495
A = []
hPa = 4e-11
card = 'default'
array_float = numpy.dtype(float)
stream = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
stream.setchannels(1)
stream.setrate(48000)
stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
stream.setperiodsize(128)
def A(f):
return (12200**2*f**4/((f**2+20.6**2)*(f**2+12200**2)*numpy.sqrt(f**2+107.7**2)*numpy.sqrt(f**2+737.9**2)))+fix_cur
def listen(self):
glob_leq = 0
liczba_ramek = 0
index_ramek = 0
while True:
try:
l, data = stream.read()
except IOError, e:
error_count += 1
print(" (%d) Error recording: %s" % (error_count, e))
else:
if l==128:
decoded_block = numpy.frombuffer(data, dtype='int16' )
else:
continue
Y = numpy.fft.fft(decoded_block) # fft computing and normalization
Aw = A(numpy.arange(20.,20000,(19980./len(Y))))
Na = Aw*Y
inverse = numpy.fft.ifft(Y)
maks = 32768
array_float = numpy.divide(inverse.real ,float( maks))
array_float = array_float**2
sum_array = numpy.sum(array_float, dtype=float)
glob_leq = glob_leq + sum_array
liczba_ramek += 1
index_ramek += 1
if index_ramek == 375:
index_ramek=0
cis_chwil = numpy.divide(glob_leq, liczba_ramek * 128)
leq =10*numpy.log10(numpy.divide(cis_chwil, hPa))
print (leq)
#A.append(leq)
#print(max(A))
A_weight().listen()
So i trying writing program compute sound pressure level with weighting A.
All work correct but when i want close may code in class I have problem. Because something wrong with invoke to importing class in this case is it alsaaudio.
I get this feedback:
Traceback (most recent call last):
File "rec_A.py", line 64, in <module>
A_weight().listen()
File "rec_A.py", line 37, in listen
l, data = stream.read()
NameError: global name 'stream' is not defined
Do you have any idea
Change each occurrence of stream to self.stream:
class A_weight():
def __init__(self):
skaler = 2.361E-14
fix_cur = 0.20565360419770495
A = []
hPa = 4e-11
card = 'default'
array_float = numpy.dtype(float)
self.stream = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
self.stream.setchannels(1)
self.stream.setrate(48000)
self.stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
self.stream.setperiodsize(128)
...
def listen(self):
glob_leq = 0
liczba_ramek = 0
index_ramek = 0
while True:
try:
l, data = self.stream.read()
...
This will make it an instance variable, and all other methods of that class (as long as they are passed the self argument) will have access to it through self.stream. See this bit of documentation for more details on instance variables.
Also, this is merely an aesthetic point, but the convention in Python is to use upper camel case for class names, i.e., AWeight instead of A_weight - but this will not affect how your code runs.

On windows: Python editor cannot import mlab from mayavi, command prompt can

I'm new to Python, but I'm having some trouble using the mayavi package on Windows. I have installed Python(x,y) and I'm using the Sublime Text 2 and Spyder editor. So far I have found that running this code from the mayavi website:
from numpy import pi, sin, cos, mgrid
dphi, dtheta = pi/250.0, pi/250.0
[phi,theta] = mgrid[0:pi+dphi*1.5:dphi,0:2*pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
x = r*sin(phi)*cos(theta)
y = r*cos(phi)
z = r*sin(phi)*sin(theta)
# View it.
from mayavi import mlab
s = mlab.mesh(x, y, z)
mlab.show()
Results in the following error:
Traceback (most recent call last):
File "C:\Dropbox\...\test.py", line 23, in <module>
from mayavi import mlab
File "C:\Python27\lib\site-packages\mayavi\mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "C:\Python27\lib\site-packages\mayavi\tools\camera.py", line 25, in <module>
from engine_manager import get_engine
File "C:\Python27\lib\site-packages\mayavi\tools\engine_manager.py", line 12, in <module>
from mayavi.preferences.api import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\api.py", line 4, in <module>
from preference_manager import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\preference_manager.py", line 29, in <module>
from traitsui.api import View, Group, Item
File "C:\Python27\lib\site-packages\traitsui\api.py", line 36, in <module>
from .editors.api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 23, in <module>
from .api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 24, in <module>
from .code_editor import CodeEditor
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 36, in <module>
class ToolkitEditorFactory ( EditorFactory ):
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 48, in ToolkitEditorFactory
mark_color = Color( 0xECE9D8 )
File "C:\Python27\lib\site-packages\traits\traits.py", line 489, in __call__
return self.maker_function( *args, **metadata )
File "C:\Python27\lib\site-packages\traits\traits.py", line 1203, in Color
return ColorTrait( *args, **metadata )
File "C:\Python27\lib\site-packages\traitsui\toolkit_traits.py", line 7, in ColorTrait
return toolkit().color_trait( *args, **traits )
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 122, in toolkit
_toolkit = _import_toolkit( toolkit_name )
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 51, in _import_toolkit
return __import__( name, globals=globals(), level=1 ).toolkit
File "C:\Python27\lib\site-packages\traitsui\wx\__init__.py", line 26, in <module>
import toolkit
File "C:\Python27\lib\site-packages\traitsui\wx\toolkit.py", line 61, in <module>
from helper \
File "C:\Python27\lib\site-packages\traitsui\wx\helper.py", line 43, in <module>
from pyface.timer.api \
File "C:\Python27\lib\site-packages\pyface\timer\api.py", line 17, in <module>
from .timer import Timer
File "C:\Python27\lib\site-packages\pyface\timer\timer.py", line 8, in <module>
from pyface.toolkit import toolkit_object
File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 73, in <module>
_init_toolkit()
File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 45, in _init_toolkit
be = import_toolkit(tk)
File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 31, in import_toolkit
__import__(be + 'init')
File "C:\Python27\lib\site-packages\pyface\ui\qt4\init.py", line 18, in <module>
from pyface.qt import QtCore, QtGui, qt_api
File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 33, in <module>
prepare_pyqt4()
File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 17, in prepare_pyqt4
sip.setapi('QDate', 2)
ValueError: API 'QDate' has already been set to version 1
[Finished in 1.5s with exit code 1]
The weird thing is, when running the mayavi code directly in command prompt, it does work! So why do I get the error in both Sublime Text 2 and in Spyder?
Thank you for taking the time to look at my question
Edit: when importing numpy completely instead of the pi, sin, cos, and mgrid commands only: the error changes to:
Traceback (most recent call last):
File "C:\Dropbox\Mijn documenten\CiTG\CIE5 - MSc Thesis\Python_TopOpPy_Christopher\testy.py", line 24, in <module>
from mayavi import mlab
File "C:\Python27\lib\site-packages\mayavi\mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "C:\Python27\lib\site-packages\mayavi\tools\camera.py", line 25, in <module>
from engine_manager import get_engine
File "C:\Python27\lib\site-packages\mayavi\tools\engine_manager.py", line 12, in <module>
from mayavi.preferences.api import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\api.py", line 4, in <module>
from preference_manager import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\preference_manager.py", line 29, in <module>
from traitsui.api import View, Group, Item
File "C:\Python27\lib\site-packages\traitsui\api.py", line 36, in <module>
from .editors.api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 23, in <module>
from .api import ArrayEditor
File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 24, in <module>
from .code_editor import CodeEditor
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 36, in <module>
class ToolkitEditorFactory ( EditorFactory ):
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 48, in ToolkitEditorFactory
mark_color = Color( 0xECE9D8 )
File "C:\Python27\lib\site-packages\traits\traits.py", line 489, in __call__
return self.maker_function( *args, **metadata )
File "C:\Python27\lib\site-packages\traits\traits.py", line 1203, in Color
return ColorTrait( *args, **metadata )
File "C:\Python27\lib\site-packages\traitsui\toolkit_traits.py", line 7, in ColorTrait
return toolkit().color_trait( *args, **traits )
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 125, in toolkit
ETSConfig.toolkit = toolkit_name
File "C:\Python27\lib\site-packages\traits\etsconfig\etsconfig.py", line 213, in _set_toolkit
"already been set to %s" % (toolkit, self._toolkit)
ValueError: cannot set toolkit to wx because it has already been set to qt4
This makes the problem seem very similar to the one in this question. The solution there does not seem feasible for Sublime Text 2, however. And the Spyder solution does not work for me because the 'Enthought Tool Suite' option is greyed out. Any help is still appreciated!
For other running into the same problem, I have found the solution. What worked for me is including the following code:
from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'qt4'
For example:
from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'qt4' # Force PyQt4 utilization
import numpy
dphi, dtheta = numpy.pi/250.0, numpy.pi/250.0
[phi,theta] = numpy.mgrid[0:numpy.pi+dphi*1.5:dphi,0:2*numpy.pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = numpy.sin(m0*phi)**m1 + numpy.cos(m2*phi)**m3 + numpy.sin(m4*theta)**m5 + numpy.cos(m6*theta)**m7
x = r*numpy.sin(phi)*numpy.cos(theta)
y = r*numpy.cos(phi)
z = r*numpy.sin(phi)*numpy.sin(theta)
# View it.
from mayavi import mlab
s = mlab.mesh(x, y, z)
mlab.show()
gives the correct result.

Multiprocessing with joblib.Parallel - error when parallizing a self written algorithm

I have a class called ftrl_proximal() which fits a model on data. It is a self written classifier (not sklearn's).
The algorithm works perfect when I run using only one CPU, but once I'm trying to perform it in multiprocessing (sort of cross validation) I get an error as described below.
The code is:
from FTRL import ftrl_proximal
from sklearn.externals import joblib
from sklearn.base import clone
import multiprocessing
from sklearn.cross_validation import StratifiedKFold
def ftrl_train(estimator, X, y, train_index, test_index):
y_ftrl_pred_test = []
y_ftrl_true = []
# Split the data to train and test
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# Fit a model on sample of the data
for idx, x in enumerate(X_train):
# predict
_p = estimator.predict(x.indices)
# update
estimator.update(x.indices, _p, y_train[idx])
for idx, x in enumerate(X_test):
_v = estimator.predict(x.indices)
y_ftrl_pred_test.append(_v) # Predicted
y_ftrl_true.append(y_test[idx]) # True
return y_ftrl_pred_test, y_ftrl_true
cv_fold = 3 # determines the number of folds.
skf = StratifiedKFold(y, n_folds=cv_fold, random_state=0)
ftrl = ftrl_proximal(alpha, beta, L1, L2, D, interaction) # initialize a learner
parallel = joblib.Parallel(n_jobs=num_cores, verbose=0, pre_dispatch='2*n_jobs')
preds_blocks = parallel(joblib.delayed(ftrl_train)(clone(ftrl), X, y,
train_index, test_index, verbose=0, fit_params=None)
for train_index, test_index in skf)
The error:
Traceback (most recent call last):
File "/home/workspace/Predictor/modelSelection.py", line 61, in <module>
class Main():
File "/home/workspace/Predictor/modelSelection.py", line 199, in Main
for train_index, test_index in skf)
File "/home/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 658, in __call__
for function, args, kwargs in iterable:
File "/home/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 184, in next
return next(self._it)
File "/home/workspace/Predictor/modelSelection.py", line 199, in <genexpr>
for train_index, test_index in skf)
NameError: global name '_ftrl' is not defined

Pynmea2 attribute error

I've been using the pynmea2 library, but today I ran a GPGGA message through it and it's throwing an attribute error when trying to access the objects datetime method.
>>> from pynmea2 import parse
>>> a = '$GPGGA,201326.000,3348.5072,N,11809.6409,W,2,20,0.55,37.5,M,-34.3,M,0000,0000*65'
>>> msg = parse(a)
>>> msg
<GGA(timestamp=datetime.time(20, 13, 26), lat='3348.5072', lat_dir='N', lon='11809.6409', lon_dir='W', gps_qual='2', num_sats='20', horizontal_dil='0.55', altitude=37.5, altitude_units='M', geo_sep='-34.3', geo_sep_units='M', age_gps_data='0000', ref_station_id='0000')>
>>> msg.datetime
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/pynmea2/nmea.py", line 154, in __getattr__
raise AttributeError(name)
AttributeError: datetime
Here's what line 154 and everything related states in nmea.py:
def __getattr__(self, name):
#pylint: disable=invalid-name
t = type(self)
try:
i = t.name_to_idx[name]
except KeyError:
raise AttributeError(name)
f = t.fields[i]
if i < len(self.data):
v = self.data[i]
else:
v = ''
if len(f) >= 3:
if v == '':
return None
return f[2](v)
else:
return v
Any ideas what this could be?
Thanks for looking at this!
Found it...GPGGA sentences have no date values in their string.
I think you want to access the timestamp attribute of the GPGGA record, using:
>>> from pynmea2 import parse
>>> a = '$GPGGA,201326.000,3348.5072,N,11809.6409,W,2,20,0.55,37.5,M,-34.3,M,0000,0000*65'
>>> msg = parse(a)
>>> msg
<GGA(timestamp=datetime.time(20, 13, 26), lat='3348.5072', lat_dir='N', lon='11809.6409', lon_dir='W', gps_qual='2', num_sats='20', horizontal_dil='0.55', altitude=37.5, altitude_units='M', geo_sep='-34.3', geo_sep_units='M', age_gps_data='0000', ref_station_id='0000')>
>>> msg.timestamp
datetime.time(20, 13, 26)