Is there any method in tensorflow like get_output in lasagne
I found that it is easy to use lasagne to make a graph like this.
import lasagne.layers as L
class A:
def __init__(self):
self.x = L.InputLayer(shape=(None, 3), name='x')
self.y = x + 1
def get_y_sym(self, x_var, **kwargs):
y = L.get_output(self.y, {self.x: x_var}, **kwargs)
return y
through the method get_y_sym
, we could get a tensor not a value, then I could use this tensor as the input of another graph.
But if I use tensorflow, how could I implement this?
1 answer

I'm not familiar with lasagne but you should know that ALL of TensorFlow uses graph based computation (unless you use tf.Eager, but that's another story). So by default something like:
net = tf.nn.conv2d(...)
returns a reference to a Tensor object. In other words,
net
is NOT a value, it is a reference to the output of the convolution node created bytf.nn.conv2d(...)
.These can then be chained:
net2 = tf.nn.conv2d(net, ...)
and so on.To get "values" one has to open a
tf.Session
:with tf.Session() as sess: net2_eval = sess.run(net2)
See also questions close to this topic

CUDA_ERROR_OUT_OF_MEMORY tensorflow
As part of my study project, I try to train a neural network which makes a segmentation on images (based on FCN), and during the execution I received the following error message:
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[1,67,1066,718] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Note that I have fixed the batch_size to 1 and I have the same error even when I tried different image sizes , I put also just 1 image to train instead of 1600 still the same error! Could you help me to solve this problem ? What is it really about ?

Error while importing tensorflow on server
I have installed tensorflow in my college server offline by downloading and then installing. In conda list tensorflow is there it shows. But when i try to import I get this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/173190025/anaconda3/lib/python3.6/sitepackages/tensorflow/__init__.py", line 22, in <module> from tensorflow.python import pywrap_tensorflow # pylint: disable=unusedimport File "/home/173190025/anaconda3/lib/python3.6/sitepackages/tensorflow/python/__init__.py", line 52, in <module> from tensorflow.core.framework.graph_pb2 import * File "/home/173190025/anaconda3/lib/python3.6/sitepackages/tensorflow/core/framework/graph_pb2.py", line 6, in <module> from google.protobuf import descriptor as _descriptor File "/home/173190025/anaconda3/lib/python3.6/sitepackages/google/protobuf/descriptor.py", line 46, in <module> from google.protobuf.pyext import _message ImportError: /usr/lib/x86_64linuxgnu/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /home/173190025/anaconda3/lib/python3.6/sitepackages/google/protobuf/pyext/../../../../../libprotobuf.so.15)

Output TFRecord to Google Cloud Storage from Python
I know
tf.python_io.TFRecordWriter
has a concept of GCS, but it doesn't seem to have permissions to write to it.If I do the following:
output_path = 'gs://mybucketname/{}/{}.tfrecord'.format(object_name, record_name) writer = tf.python_io.TFRecordWriter(output_path) # write to writer writer.close()
then I get 401s saying "Anonymous caller does not have storage.objects.create access to mybucketname."
However, on the same machine, if I do
gsutil rsync d r gs://mybucketname bucketbackup
, it properly syncs it, so I've authenticated properly using gcloud.How can I give
TFRecordWriter
permissions to write to GCS? I'm going to just use Google's GCP python API for now, but I'm sure there's a way to do this using TF alone. 
Unable to use Theano Keras backend inside Google Cloud DL VM
I'm trying to run Keras on a Google Cloud Deep Learning VM instance using the Theano backend instead of the default Tensorflow one. I can ssh into the instance as the
jupyter
user and subsequently runpip install theano
, just trying to import theano from the python prompt also works, but when I set the Keras backend to Theano in Jupyter Lab (viaos.environ['KERAS_BACKEND'] = 'theano'
), anytime I try to train a model I receive this strange error message:ERROR (theano.gof.cmodule): [Errno 2] No such file or directory: '/opt/anaconda1anaconda2anaconda3/lib'
What's up with that?

Keras theano prediction using cpu
I am working on my university cluster and I am trying to import my keras model into an analyzer to get some predictions.
The cluster only has cpu's and I don't have access to CUDA or any GPU's for running my model and making predictions.
I am currently able to make predictions by loading the model json and weights, but I get the following message while execution:
cc1plus: fatal error: cuda_runtime.h: No such file or directory compilation terminated. ERROR:theano.sandbox.cuda] Failed to compile cuda_ndarray.cu
I have seen others have same error but the solution doesn't apply to me because I do not have /user/local/cuda/ and I can't change it.
I set the device to cpu hoping this was the problem but it's not the case:
theano.config.device='cpu'
What should I do?

Incorrect behavior with convolution in PyMC3/Theano
My initial work just trying to infer the appropriate scaling for a convolved signal and filter shows far too much noise and low scaling values. Inference of a scale parameter works with a linear regression.
Hi! I'm new to python, PyMC3, and general Bayesian analysis so I may have missed some basic issues and I would really appreciate some help. I have an electronic system I am trying to characterize via a single filter. An input signal is convolved with the filter to get the measured output. I have a separate, handanalysis that yields a result, but its less than satisfying and I want to try Bayesian approaches.
I wanted to make sure I was understanding the different steps involved before diving further, so I made a dummy test case. I've created an arbitrary filter function which is convolved with the input signal. To introduce some inference variables, I multiply the result by a scaling parameter and then my likelihood function is a Normal distribution about the convolved signal with a standard deviation to be compared to the observation. Since I created the data, I know what answers should be obtained. I can do the same process with a linear regression and it correctly obtains the correct scaling and standard deviation for the noise.
import numpy as np import matplotlib.pyplot as plt import pymc3 as pm import pandas as pd import theano.tensor as tt import theano import theano.tensor.signal.conv #theano.config.compute_test_value = 'off' #create source function dtbase = 0.5e9 tbase = np.arange(0, 1e7, dtbase) set1 = ((tbase > 5e9) & (tbase < 45e9)) sig1 = np.zeros((len(tbase),1)) sig1[set1,0] = 1 set2 = ((tbase > 5e9) & (tbase < 8e9)) sig2 = np.zeros((len(tbase),1)) sig2[set2,0] = 1 #define the time base of the transfer function t2 = np.arange(0, 50e9, dtbase) hfunc = np.zeros((len(t2),1)) mu1 = 2e9 sd1 = 0.5e9 g1 = np.exp(0.5*(t2  mu1)**2/sd1**2) RC1 = 2e9 A1 = 1 mu2 = 2e9 RC2 = 20e9 A2 = 0.02 set3 = (t2 > mu2) hfunc[set3,0]= A1*np.exp(1*t2[set3]/RC1) + A2*np.exp(1*t2[set3]/RC2) hfunc2 = np.convolve(hfunc[:,0], g1) hfunc2 = hfunc2[0:len(t2)]/np.trapz(hfunc2) hproto = np.zeros((len(hfunc2),1)) hproto[:,0] = hfunc2 hfunc3 = hfunc2/(max(hfunc2)) #create the observed data arrays obs1 = np.convolve(sig1[:,0], hfunc2) obs1 = obs1[0:len(tbase)] noiseAmp = np.random.normal(0, 0.005*max(obs1), len(tbase)) obs1 += noiseAmp obs2 = np.convolve(sig2[:,0], hfunc2) obs2 = obs2[0:len(tbase)] noiseAmp2 = np.random.normal(0, 0.005*max(obs1), len(tbase)) obs2 += noiseAmp2 #define the theano convolution function conv2d = tt.signal.conv.conv2d x = tt.dmatrix('x') x.tag.test_value = np.random.rand(5,1) y = tt.dmatrix('y') y.tag.test_value = np.random.rand(5,1) veclen = x.shape[0] conv1d_expr = conv2d(x, y, \ image_shape=(1, veclen), \ border_mode='full') conv1d = theano.function([x, y], \ outputs=conv1d_expr) #append the two signals to process at the same time xapp = np.append(sig1, sig2) obsapp = np.append(obs1, obs2) basic_model = pm.Model() with basic_model: scale = pm.Normal('scale', mu=0, sd = 5) Yobs_a = conv1d(sig1, hproto) Yobs_b = conv1d(sig2, hproto) #Yobs = (Yobs_a[0:len(sig1)])*(1scale) Ydelta_a = Yobs_a[0:len(sig1)]*(1scale) Ydelta_b = Yobs_b[0:len(sig2)]*(1scale) #Ydelta = th_append(Ydelta_a, Ydelta_b) Ydelta = tt.concatenate([Ydelta_a, Ydelta_b], axis=0) sigma = pm.HalfCauchy('sigma', 1) obs = pm.Normal('obs', mu=Ydelta, sd=sigma, observed=obsapp) trace = pm.sample(800, tune=800, cores=1, jobs=1, chains=2)
Now, if I use:
ppc = pm.sample_posterior_predictive(trace, samples=100, model=basic_model) for i in range(100): plt.plot((ppc['obs'][i,:]), 'b', alpha=0.05) plt.plot(obsapp, 'r', linewidth=1)
I get a very noisy set of posteriors because my solver is coming back with a value of:
mean sd mc_error hpd_2.5 hpd_97.5 n_eff Rhat scale 0.72 0.0 0.0 0.72 0.73 1549.32 1.0 sigma 0.38 0.0 0.0 0.38 0.38 1641.81 1.0
whereas scale should be 0 and sigma should be 0.005!
If I run the same algorithm using a scaled linear regression as the "observed", this method comes back with very precise values.
Clearly I'm missing something but I don't know where to look.

Error with 3dimensional weights in Lasagne
I am trying to implement a hierarchical neural net in Lasagne and Python. My weights from the input layer to the first hidden layer are 3D, but when I add them in Lasagne Input Layer I get the following error:
ValueError: cannot initialize parameter W: the provided Theano variable has 3 dimensions, should be 2.
Can anyone help?

Critic never converges in A2C
I'm trying to implement A2C with Lasagne+Theano (Python) to solve standard OpenAI gym problems. However my code doesn't seem to converge to anything useful.
I've already tried various things: I've played with hyperparameters, trying various values for the learning rate and the network topology for both the actor and the critic, I've tried to use the same network with just two output layers for both the actor and the critic together (and summing their losses), as done by many, I've tried using or not gradient clipping, I've tried different gradient descent method. If I get rid of the critic and I use the discounted return as the signal to drive my policy gradient (REINFORCE algorithm) it works fine. Just plugging in the critic again makes things go wrong. If I get rid of the actor and adapt the critic architecture into a DQN (thus having an output for each action instead of a single one), it works fine.
Here it is the code for the network creation and the actual training process.
from gym.spaces import Box, Discrete import lasagne import numpy as np class ActorNetwork: def __init__(self, x, observation_space, action_space, num_hidden = 2, hidden_units = [64, 64]): assert type(observation_space) == Box, "Incompatible observation_space type." assert type(action_space) == Discrete, "Incompatible action_space type." assert len(hidden_units) == num_hidden, "Number of hidden layers and dimensions mismatch." net = lasagne.layers.InputLayer([None] + list(observation_space.shape), x) for l in range(num_hidden): net = lasagne.layers.DenseLayer(net, hidden_units[l], nonlinearity = lasagne.nonlinearities.leaky_rectify) self.out = lasagne.layers.DenseLayer(net, action_space.n, nonlinearity = lasagne.nonlinearities.softmax) def get_output(self): return lasagne.layers.get_output(self.out) def get_param_values(self): return lasagne.layers.get_all_param_values(self.out) def set_param_values(self, values): return lasagne.layers.set_all_param_values(self.out, values) def get_params(self): return lasagne.layers.get_all_params(self.out, trainable = True) def save_model(self, name): np.save(name, lasagne.layers.get_all_param_values(self.out)) def load_model(self, name): lasagne.layers.set_all_param_values(self.out, np.load(name)) class CriticNetwork: def __init__(self, x, observation_space, num_hidden = 2, hidden_units = [64, 64]): assert type(observation_space) == Box, "Incompatible observation_space type." assert len(hidden_units) == num_hidden, "Number of hidden layers and dimensions mismatch." net = lasagne.layers.InputLayer([None] + list(observation_space.shape), x) for l in range(num_hidden): net = lasagne.layers.DenseLayer(net, hidden_units[l], nonlinearity = lasagne.nonlinearities.leaky_rectify) self.out = lasagne.layers.DenseLayer(net, 1, nonlinearity = lasagne.nonlinearities.linear) def get_output(self): return lasagne.layers.get_output(self.out) def get_param_values(self): return lasagne.layers.get_all_param_values(self.out) def set_param_values(self, values): return lasagne.layers.set_all_param_values(self.out, values) def get_params(self): return lasagne.layers.get_all_params(self.out, trainable = True) def save_model(self, name): np.save(name, lasagne.layers.get_all_param_values(self.out)) def load_model(self, name): lasagne.layers.set_all_param_values(self.out, np.load(name))
import time from gym.spaces import Box, Discrete import lasagne import numpy as np import theano from theano import tensor as T from theano.gradient import disconnected_grad import gym import gym.wrappers from Networks import ActorNetwork, CriticNetwork def discount_rewards(rewards, gamma): discounted_rewards = [] rew_sum = 0. for r in reversed(rewards): rew_sum = r + gamma * rew_sum discounted_rewards.append(rew_sum) return discounted_rewards[::1] def target_network_update(original, target): values = original.get_param_values() target.set_param_values(values) learning_rate = 7e4 learning_rate_cc = 7e4 gamma = 0.99 ent_lambda = 1e2 lambda_gae = 1.0 gae = True grad_clip = 0.5 training_episodes = 10000 episode_max_length = 1000 target_update_every = 100 print_freq = 100 render = False env = gym.make("CartPolev0") #env = gym.wrappers.Monitor(gym.make("CartPolev0"), directory="videos", force=True) obs_t = T.dmatrix("obs_t") critic_targets_t = T.dvector("critic_targets_t") advantages_t = T.dvector("advantages_t") act_t = T.ivector("act_t") actor = ActorNetwork(obs_t, env.observation_space, env.action_space) actor_out = actor.get_output() actions_prob = theano.function([obs_t], actor_out, allow_input_downcast = True) critic = CriticNetwork(obs_t, env.observation_space) critic_target = CriticNetwork(obs_t, env.observation_space) target_network_update(critic, critic_target) critic_out = critic.get_output() target_out = critic_target.get_output() value_function = theano.function([obs_t], critic_out, allow_input_downcast = True) value_target = theano.function([obs_t], target_out, allow_input_downcast = True) critic_loss = T.mean(lasagne.objectives.squared_error(disconnected_grad(critic_targets_t), critic_out)) critic_params = critic.get_params() critic_grads = lasagne.updates.total_norm_constraint(T.grad(critic_loss, critic_params), grad_clip) critic_updates = lasagne.updates.rmsprop(critic_grads, critic_params, learning_rate_cc) train_critic = theano.function([obs_t, critic_targets_t], updates = critic_updates, allow_input_downcast = True) actor_loss = T.mean(T.log(actor_out[T.arange(act_t.shape[0]),act_t] + 1e20) * disconnected_grad(advantages_t) + ent_lambda * (np.log(actor_out + 1e20) * actor_out).sum(1)) actor_params = actor.get_params() actor_grads = lasagne.updates.total_norm_constraint(T.grad(actor_loss, actor_params), grad_clip) actor_updates = lasagne.updates.rmsprop(actor_grads, actor_params, learning_rate) train_actor = theano.function([obs_t, advantages_t, act_t], updates = actor_updates, allow_input_downcast = True) start = time.time() episodes_rewards = [] for e in range(training_episodes): ep_start = time.time() obs_batch = [] new_obs_batch = [] actions_batch = [] rewards_batch = [] dones_batch = [] obs = env.reset() for t in range(episode_max_length): if render: env.render() obs_batch.append(obs) action = np.random.choice(env.action_space.n, p = actions_prob([obs])[0]) actions_batch.append(action) new_obs, rewards, dones, _ = env.step(action) new_obs_batch.append(new_obs) rewards_batch.append(rewards) dones_batch.append(dones) obs = new_obs if dones: break episodes_rewards.append(sum(rewards_batch)) if e % print_freq == 0: print("Episode {}/{} ended in {} steps: mean reward is {}".format(e, training_episodes, t + 1, np.mean(episodes_rewards[print_freq:]))) if np.mean(episodes_rewards[100:]) >= env.spec.reward_threshold: print("You won!") break critic_targets = rewards_batch + gamma * np.squeeze(value_target(new_obs_batch)) * np.logical_not(dones_batch) train_critic(obs_batch, critic_targets) advantages = rewards_batch + gamma * np.squeeze(value_function(new_obs_batch)) * np.logical_not(dones_batch)  np.squeeze(value_function(obs_batch)) if gae: advantages = discount_rewards(advantages, gamma * lambda_gae) train_actor(obs_batch, advantages, actions_batch) if e % target_update_every == 0: target_network_update(critic, critic_target) print("Total training time: {}".format(round(time.time()  start, 3))) #env.close() actor.save_model("actor") critic.save_model("critic")
Performances on the very simple CartPole environment are horrible (it can jiggle the pole for like 10 steps, while with both DQN and REINFORCE I was easily able to consistently reach 200 and solve the game). Any suggestion on what am I doing wrong in the code?

'dict_values' object does not support indexing
I am using this python 2 code: https://github.com/LouisFoucard/MC_DCNN/blob/master/.ipynb_checkpoints/MultiChannel_DeepConvNetcheckpoint.ipynb
When I run the code in python 3, I get the error that:
'dict_values' object does not support indexing
This error is corresponding to this line:
network[i] = InputLayer(shape=(batch_size,1,parameters.FREQ_WINDOW),input_var=input_var[i],name="input_layer_1")
So, I changed
input_var=input_var[i]
toinput_var=None
.Do you think it is the way that this error should be solved?
Thanks in advance