Why is TensorFlow 2 much slower than TensorFlow 1?

It’s been cited by many users as the reason for switching to Pytorch, but I’ve yet to find a justification/explanation for sacrificing the most important practical quality, speed, for eager execution. Below is code benchmarking performance, TF1 vs. TF2 – with TF1 running anywhere from 47% to 276% faster. My question is: what is it, … Read more

What is the pythonic way to avoid default parameters that are empty lists?

Sometimes it seems natural to have a default parameter which is an empty list. Yet Python produces unexpected behavior in these situations. If for example, I have a function: def my_func(working_list=[]): working_list.append(“a”) print(working_list) The first time it is called, the default will work, but calls after that will update the existing list (with one “a” … Read more

AttributeError(“‘str’ object has no attribute ‘read'”)

In Python I’m getting an error: Exception: (<type ‘exceptions.AttributeError’>, AttributeError(“‘str’ object has no attribute ‘read'”,), <traceback object at 0x1543ab8>) Given python code: def getEntries (self, sub): url=”http://www.reddit.com/” if (sub != ”): url += ‘r/’ + sub request = urllib2.Request (url + ‘.json’, None, {‘User-Agent’ : ‘Reddit desktop client by /user/RobinJ1995/’}) response = urllib2.urlopen (request) jsonStr … Read more

Set Colorbar Range in matplotlib

I have the following code: import matplotlib.pyplot as plt cdict = { ‘red’ : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)), ‘green’: ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)), ‘blue’ : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45)) } cm = m.colors.LinearSegmentedColormap(‘my_colormap’, cdict, 1024) plt.clf() plt.pcolor(X, … Read more

How do I tell Matplotlib to create a second (new) plot, then later plot on the old one?

I want to plot data, then create a new figure and plot data2, and finally come back to the original plot and plot data3, kinda like this: import numpy as np import matplotlib as plt x = arange(5) y = np.exp(5) plt.figure() plt.plot(x, y) z = np.sin(x) plt.figure() plt.plot(x, z) w = np.cos(x) plt.figure(“””first figure”””) … Read more

How do I correctly setup and teardown for my pytest class with tests?

I am using selenium for end to end testing and I can’t get how to use setup_class and teardown_class methods. I need to set up browser in setup_class method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_class method. But logically it seems like a bad solution, because … Read more

“TypeError: (Integer) is not JSON serializable” when serializing JSON in Python?

I am trying to send a simple dictionary to a json file from python, but I keep getting the “TypeError: 1425 is not JSON serializable” message. import json alerts = {‘upper’:[1425],’lower’:[576],’level’:[2],’datetime’:[‘2012-08-08 15:30’]} afile = open(‘test.json’,’w’) afile.write(json.dumps(alerts,encoding=’UTF-8′)) afile.close() If I add the default argument, then it writes, but the integer values are written to the json … Read more