Obsolete/erroneous code in convolutional_mlp.py at DeepLearningTutorials? - neural-network

This code contains the following tidbit:
from theano.tensor.nnet import conv2d
...
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
input_shape=image_shape
)
which raises an exception due to 'input_shape' not being found, despite being mentioned in the documentation where it says that:
"image_shape ... – Deprecated alias for input_shape"
Looking at conv.py both locally and in the source I found:
def conv2d(input, filters, image_shape=None, filter_shape=None,
border_mode='valid', subsample=(1, 1), **kargs):
Needless to say, there is no trace of input_shape.
If one modifies the code above as follows
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape
)
, the exception disappears and the code runs fine.
What am I missing? If image_shape is deprecated, how come it works while input_shape does not?
Is the theano version at the repository obsolete?
PS: I would have liked to ask directly the folks at http://deeplearning.net, but I could not find how.

Are you sure you have the latest version installed?
conv.py contains the deprecated implementation of conv2d. The new implementation can be found in __init__.py
Make sure you are using the import statement
from theano.tensor.nnet import conv2d
and not
from theano.tensor.nnet.conv import conv2d
since the second one is going to import the deprecated implementation

Related

How to apply a decorator with arguments to an imported function

This similar to a previous question but not quite.
Like the other question, I want to apply a decorator to an imported function, but in my case, my decorator requires arguments
Working
from flask_restx import Namespace
from . import exceptions as e
api = Namespace('v2', 'API Version 2' )
#api.errorhandler(e.MissingPrompt)
def handle_bad_requests(error):
'''Namespace error handler'''
logger.warning(error.log)
return({'message': error.specific}, error.code)
But I want to move handle_bad_requests() to its own file
so I want it like
from flask_restx import Namespace
from . import exceptions as e
api = Namespace('v2', 'API Version 2' )
#api.errorhandler(e.MissingPrompt)
e.handle_bad_requests # ??
I tried like the other answers here suggested like this
handle_bad_requests = api.errorhandler(e.handle_bad_requests, e.MissingPrompt)
But that just gives me an error I am sending too many arguments
I just discovered the answer nested within the comments of a previous answer
handle_bad_requests = api.errorhandler(e.MissingPrompt)(e.handle_bad_requests)
And that seems to work fine.
The double parentheses would never have occured to me

How can I use a Session attribute saved from a Check in an SSE request in Gatling?

See working Java code at bottom.
I'm trying to create an SSE connection testing scenario that uses a client ID that is the response to a previous REST POST call. Here's the Scala code:
package computerdatabase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class ssetest extends Simulation {
val registration = scenario("registration")
.exec(http("endpoint1")
.post("http://localhost:7070/api/register")
.body(RawFileBody("./Stocks.json")).asJson
.check(status is 201)
.check(jsonPath("$.id").saveAs("clientId")))
setUp(scn.inject(constantUsersPerSec(200).during(180))).assertions(global.failedRequests.count.is(0))
val sseConnection = scenario("ServerSentEvents")
.exec { session =>
sse("GET messages")
.connect("/api/sse/" + session("clientId").as[String])
.await(1)(
sse.checkMessage("ConnectionCheck").matching(substring("connection established"))
.check(bodyString.saveAs("InitialMessage"))
)
}
}
I would like to use the client ID saved into the Session in the first scenario, to configure the SSE endpoint to connect to in the second one. Presently, when running the scenario, I'm getting:
19:35:50.086 [ERROR] i.g.c.ZincCompiler$ - /home/dingo/Downloads/gatling-charts-highcharts-bundle-3.7.6/user-files/simulations/computerdatabase/SseSimulation.scala:21:15: type mismatch;
found : io.gatling.http.request.builder.sse.SseConnectRequestBuilder
required: io.gatling.commons.validation.Validation[io.gatling.core.session.Session]
.await(1)(
^
19:35:50.135 [ERROR] i.g.c.ZincCompiler$ - one error found
19:35:50.145 [ERROR] i.g.c.ZincCompiler$ - Compilation crashed
xsbt.InterfaceCompileFailed: null
at xsbt.CachedCompiler0.handleErrors(CompilerBridge.scala:183)
at xsbt.CachedCompiler0.run(CompilerBridge.scala:172)
at xsbt.CachedCompiler0.run(CompilerBridge.scala:134)
at xsbt.CompilerBridge.run(CompilerBridge.scala:39)
at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:192)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
at sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:247)
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:182)
at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:163)
at sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
at sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:163)
at sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:210)
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
at sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:177)
at sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:175)
at sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:461)
at sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
at sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263)
at sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:416)
at sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:503)
at sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:403)
at sbt.internal.inc.Incremental$.apply(Incremental.scala:169)
at sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
... 5 common frames omitted
Wrapped by: sbt.internal.inc.CompileFailed: Compilation failed
at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:335)
at sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
at sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
at io.gatling.compiler.ZincCompiler$.doCompile(ZincCompiler.scala:218)
at io.gatling.compiler.ZincCompiler$.main(ZincCompiler.scala:226)
at io.gatling.compiler.ZincCompiler.main(ZincCompiler.scala)
How can I fix this?
Disclaimer: I don't know Scala.
Actions taken:
Created a new folder structure for Gatling in my project
Put the simulation class in ./src/gatling/java/computerdatabase
Put Stocks.json in ./src/gatling/resources/Stocks.json and made the file path in the code absolute
Added the Gatling Gradle plugin to build.gradle
Checked this example Simulation for inspiration
Fiddled around a bit with the code, reading about Session function syntax
package computerdatabase;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
public class SseSimulation extends Simulation {
HttpProtocolBuilder httpProtocol = http
.baseUrl("http://localhost:7070");
ScenarioBuilder scn = scenario("Scenario Name")
.exec(http("client id registration")
.post("/api/register/")
.body(RawFileBody("Stocks.json")).asJson()
.check(status().is(201))
.check(jsonPath("$.id").saveAs("clientId")))
.exec(
sse("open channel")
.connect(session -> "/api/sse/" + session.getString("clientId"))
);
{
setUp(scn.injectOpen(constantUsersPerSec(200).during(1)).protocols(httpProtocol)).assertions(global().failedRequests().count().is(0L));
}
}
Disclaimer: I don't know Scala.
Then switch to Java. Gatling 3.7 (that you're using) supports it as well and both the official documentation and the official online courses explain it very well.
Lots of things wrong:
the code you've provided can't possibly compile. There's nothing named scn in there. Please fix it so one can help you.
your compile error has nothing to do with the piece of code you've provided. The compiler complains about an error in SseSimulation while you've provided a class named ssetest. Again, please first fix your code sample
RawFileBody("./Stocks.json") is wrong. You shouldn't use a relative path. Let the ClassLoader resolve your files. This Stocks.json file should go in user-files/resources and you should write RawFileBody("Stocks.json")
Checks save data into the Session of the virtual user that executed the request. You can't use different scenarios because they would execute different sets of virtual users. You have to perform register and SSE connect in the same scenario.
I recommend that you should invest some time in reading the documentation and checking the online courses, you will ultimately save a lot of time.

How to get clickable links to functions and classes on VS code for Mac

Sorry if the question is not entirely clear. Will add details if unclear.
PrintPlugin is a class from another python file I have imported. I used to get clickable links on this on VS code. Meaning, if I cmd+click on PrintPlugin it would go to that class on the source Python file.
My bottom bar used to look like this when it was working.
Now it looks like this
The difference is that I was having the Python 3.9.5 64-bit ('base':conda) earlier.
Everything else works fine. I am on my virtual environment too. I am not sure what else could be causing this. Will add any details required.
Edit:
I have added some reproducible code. This differs from the original image because the original was work related and I can't post it here. But I'm still running this in the same folder and in the same virtual environment, so it has the same problem (that I cannot cmd+click on Multiply() in process.py and go to the class on plugin.py)
File process.py
import numpy as np
from plugin import Multiply
a = np.array([[2,3],
[3,4]])
b = np.array([[5,6],
[7,8]])
result = Multiply()
print(result.multiply(a,b))
File plugin.py
import numpy as np
class Multiply():
def __init__(
self,
a = np.array([[1,1],
[1,1]]),
b = np.array([[1,1],
[1,1]])
):
return
def multiply(self,a,b):
c = a # b
return c
Output when I do python process.py
>>> [[31 36]
[43 50]]

AttributeError for selfloop_edges()

When executing the following:
import networkx as nx
import matplotlib.pyplot as plt
import csv
with open("nutrients.csv") as file:
reader = csv.reader(file)
G = nx.Graph(reader) #initialize Graph
print(G.nodes()) #this part works fine
print(repr(G.edges))
G.selfloop_edges()#attribute of question
It's coming back with
AttributeError:"Graph" object has no attribute 'selfloop_edge'
Does anyone know what could be the issue?
You getting an error because this method has been moved from the base graph class into the main namespace, see Migration guide from 1.X to 2.0. So either you're looking at the docs of 1.X or using code from previous releases.
You need to call this method as:
nx.selfloop_edges(G, data=True)

How to get PyTest fixtures to autocomplete in PyCharm (type hinting)

I had a bear of a time figuring this out, and it was really bugging me, so I thought I'd post this here in case anyone hit the same problem...
(and the answer is so dang simple it hurts :-)
The Problem
The core of the issue is that sometimes, not always, when dealing with fixtures in PyTest that return objects, when you use those fixtures in a test in PyCharm, you don't get autocomplete hints. If you have objects with large numbers of methods you want to reference while writing a test, this can add a lot of overhead and inconvenience to the test writing process.
Here's a simple example to illustrate the issue:
Let's say I've got a class "event_manager" that lives in:
location.game.events
Let's further say that in my conftest.py file (PyTest standard thing for the unfamiliar), I've got a fixture that returns an instance of that class:
from location.game.events import event_manager
...
#pytest.fixture(scope="module")
def event_mgr():
"""Creates a new instance of event generate for use in tests"""
return event_manager()
I've had issues sometimes, (but not always - I can't quite figure out why) with classes like this where autocomplete will not work properly in the test code where I use the fixture, e.g.
def test_tc10657(self, evt_mgr):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This does not offer autocomplete hints when you type ".")
So the answer is actually quite simple, once you review type hinting in PyCharm:
http://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
Here's how to fix the above test code so that autocomplete works properly:
from location.game.events import event_manager
...
def test_tc10657(self, evt_mgr: event_manager):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This DOES offer hints when you type "." Yay!)
Notice how I explicitly type the fixture as an input parameter of type event_manager.
Also if you add a docstring to a function and specify the type of the the parameters, you will get the code completion for those parameters.
For example using pytest and Selenium:
# The remote webdriver seems to be the base class for the other webdrivers
from selenium.webdriver.remote.webdriver import WebDriver
def test_url(url, browser_driver):
"""
This method is used to see if IBM is in the URL title
:param WebDriver browser_driver: The browser's driver
:param str url: the URL to test
"""
browser_driver.get(url)
assert "IBM" in browser_driver.title
Here's my conftest.py file as well
import pytest
from selenium import webdriver
# Method to handle the command line arguments for pytest
def pytest_addoption(parser):
parser.addoption("--driver", action="store", default="chrome", help="Type in browser type")
parser.addoption("--url", action="store", default='https://www.ibm.com', help="url")
#pytest.fixture(scope='module', autouse=True)
def browser_driver(request):
browser = request.config.getoption("--driver").lower()
# yield the driver to the specified browser
if browser == "chrome":
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
else:
raise Exception("No driver for browser " + browser)
yield driver
driver.quit()
#pytest.fixture(scope="module")
def url(request):
return request.config.getoption("--url")
Tested using Python 2.7 and PyCharm 2017.1. The docstring format is reStructuredText and the "Analyze Python code in docstrings" checkbox is checked in settings.