Pyomo objective definition error: "SyntaxError: Generator expression must be parenthesized" - pyomo

I am new to Pyomo, and am getting an error when I try to declare my objective. I would prefer to use a def for the objective rather than expr= if possible.
The code is below.
# Import pyomo
from pyomo.environ import *
N = ['Harlingen', 'Memphis', 'Ashland']
M = ['NYC', 'LA', 'Chicago', 'Houston']
d = {('Harlingen', 'NYC'): 1956, \
('Harlingen', 'LA'): 1606, \
('Harlingen', 'Chicago'): 1410, \
('Harlingen', 'Houston'): 330, \
('Memphis', 'NYC'): 1096, \
('Memphis', 'LA'): 1792, \
('Memphis', 'Chicago'): 531, \
('Memphis', 'Houston'): 567, \
('Ashland', 'NYC'): 485, \
('Ashland', 'LA'): 2322, \
('Ashland', 'Chicago'): 324, \
('Ashland', 'Houston'): 1236 }
P = 2
model = ConcreteModel("warehouse location problem")
model.N = Set(dimen=1, initialize=N)
model.M = Set(dimen=1, initialize=M)
model.d = Param(model.N, model.M, within=PositiveIntegers, initialize=d)
model.P = Param(initialize=P)
model.y = Var(model.N, within=PositiveIntegers)
model.x = Var(model.N, model.M, bounds=(0,1))
def obj_rule(model):
return sum(model.d[n,m] * model.x[n,m] for n in model.N, m in model.M)
model.obj = Objective(rule=obj_rule)
The error I receive is
File "warehouseLocation.py", line 35
return sum(model.d[n,m] * model.x[n,m] for n in model.N, m in model.M)
^
SyntaxError: Generator expression must be parenthesized
All help appreciated

Write your objective function in this way
def obj_rule(model):
return sum(model.d[n,m]* model.x[n,m] for n in model.N for m in model.M)
model.obj = Objective(rule=obj_rule)
To check it
model.obj.pprint()

Related

Defining mutable matrix and manually setting the values during run

So my interest is to generate a hessian and gradient during a run to evaluate a constraint -
def shifted_obj(model):
for i in range(1,1+model.m_set):
M.g_shift[i] =((-1)**(i-1))*math.cos(model.v[i])
for i in range(1,1+model.m_set+1):
for j in range(1,1+model.m_set+1):
if(i==j):
model.H_shift[i,j] = -((-1)**(i-1))*math.sin(model.v[i])
return .5*sum(model.g_shift[ i ] * model.g_shift[ i ] for i in model.m_set) + .5*sum( model.g_shift[i]*model.H_shift[i,j]*model.pk[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H_shift[j,i]*model.g_shift[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H_shift[j,i]*model.H_shift[i,j]*model.pk[j] for i in model.m_set for j in model.m_set )
I set two mutable parameters, and I try to index over them to set them to the correct value but looks like this isn't the way to do this:
ERROR: Rule failed when generating expression for constraint de_constraint2:
TypeError: unsupported operand type(s) for +: 'int' and
'FiniteSimpleRangeSet'
ERROR: Constructing component 'de_constraint2' from data=None failed:
TypeError: unsupported operand type(s) for +: 'int' and
'FiniteSimpleRangeSet'
Traceback (most recent call last):
File "line_search.py", line 199, in <module>
line_search(init_x,eps)
File "line_search.py", line 156, in line_search
instance = M.create_instance()
File "/Users/drvogt/opt/anaconda3/lib/python3.7/site-packages/pyomo/core/base/PyomoModel.py", line 726, in create_instance
profile_memory=profile_memory )
File "/Users/drvogt/opt/anaconda3/lib/python3.7/site-packages/pyomo/core/base/PyomoModel.py", line 783, in load
profile_memory=profile_memory)
File "/Users/drvogt/opt/anaconda3/lib/python3.7/site-packages/pyomo/core/base/PyomoModel.py", line 834, in _load_model_data
self._initialize_component(modeldata, namespaces, component_name, profile_memory)
File "/Users/drvogt/opt/anaconda3/lib/python3.7/site-packages/pyomo/core/base/PyomoModel.py", line 885, in _initialize_component
declaration.construct(data)
File "/Users/drvogt/opt/anaconda3/lib/python3.7/site-packages/pyomo/core/base/constraint.py", line 755, in construct
tmp = _init_rule(_self_parent)
File "line_search.py", line 152, in <lambda>
sufficentprogress_rule = lambda M: sufficentprogress(M) <=0.0
File "line_search.py", line 80, in sufficentprogress
return shifted_obj(model) - extra_term - line_obj(model)
File "line_search.py", line 66, in shifted_obj
for i in range(1,1+model.m_set):
TypeError: unsupported operand type(s) for +: 'int' and 'FiniteSimpleRangeSet'
If someone can tell me how to manually set these fields I would appreciate it. Also do I have to do some reconstruct after I set these matrices? I think I may because this function is being called in the de_constraint2constraint.
Here is my entire code for context, thank you.
from pyomo.core import *
from pyomo.environ import *
import numpy as np
import random
import math
#More Thuente linesearch
def objective_func(xk):
x_len = len(xk)
my_sum=0.0
for i in range(1,x_len+1):
#my_sum = my_sum + ((-1)**(i-1))*(xk[i-1]-1)**2
my_sum = my_sum + ((-1)**(i-1))*math.sin(xk[i-1])
return my_sum
def grad_func(xk):
x_len = len(xk)
grad ={}
for i in range(1,len(xk)+1):
grad[i] = ((-1)**(i-1))*math.cos(xk[i-1])
return grad
# grad=np.zeros((x_len,1))
# my_sum=0.0
# for i in range(0,x_len):
# grad[i,0]= ((-1)**i)*2.0*(xk[i]-1)
# return grad
def hess_func(xk):
# H = np.zeros((len(xk),len(xk)))
# for i in range(0,len(xk)):
# H[i,i] = ((-1)**i)*2.0
# return H
H = {}
for i in range(1,len(xk)+1):
for j in range(1,len(xk)+1):
#print((i,j))
if(i==j):
H[(i,j)] = -((-1)**(i-1))*math.sin(xk[i-1])
else:
H[(i,j)] = 0.0
return H
def convert_xk(xk):
new_xk = {}
for i in range(1,len(xk)+1):
new_xk[i] = xk[i-1]
return new_xk
def line_search(init_x,eps):
def c_param_init ( model, v ):
grad_set=init_grad
return grad_set[ v -1 ]
def x_param_init ( model, v ):
return init_x[ v -1 ]
def hessian_init(model,v1,v2):
return init_hessian[ v1 -1 ][ v2 -1 ]
def line_obj(model):
return .5*sum(model.g[ i ] * model.g[ i ] for i in model.m_set) + .5*sum( model.g[i]*model.H[i,j]*model.pk[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H[j,i]*model.g[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H[j,i]*model.H[i,j]*model.pk[j] for i in model.m_set for j in model.m_set )
def shifted_obj(model):
for i in range(1,1+model.m_set):
M.g_shift[i] =((-1)**(i-1))*math.cos(model.v[i])
for i in range(1,1+model.m_set+1):
for j in range(1,1+model.m_set+1):
if(i==j):
model.H_shift[i,j] = -((-1)**(i-1))*math.sin(model.v[i])
return .5*sum(model.g_shift[ i ] * model.g_shift[ i ] for i in model.m_set) + .5*sum( model.g_shift[i]*model.H_shift[i,j]*model.pk[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H_shift[j,i]*model.g_shift[j] for i in model.m_set for j in model.m_set ) + .5*sum( model.pk[i]*model.H_shift[j,i]*model.H_shift[i,j]*model.pk[j] for i in model.m_set for j in model.m_set )
def sufficentprogress(model):
extra_term = .0001*model.alpha[1]*sum(model.g[i] * model.pk[i] for i in model.m_set)
print("YO")
print(extra_term)
return shifted_obj(model) - extra_term - line_obj(model)
def build_line_constraint(M,i):
return (M.v[i] - M.xk[i] - M.alpha[1]*M.pk[i]==0.0)
counter=0
curr_xk = init_x
#print(curr_xk)
#current_grad = grad_func(curr_xk)
#current_grad=current_grad.tolist()
#current_H = hess_func(curr_xk)
#print(current_H)
#current_H = current_H.tolist()
#print(current_H)
first_obj = objective_func(init_x)
#print(first_obj)
obj_list=[]
while(counter <10):
current_grad = grad_func(curr_xk)
#print(current_grad)
current_H = hess_func(curr_xk)
#current_grad = current_grad.tolist()
#current_H=current_H.tolist()
# print(curr_xk)
# print(current_grad)
# temp_grad = []
# for i in range(0,len(current_grad)):
# temp_grad.append(current_grad[i][0])
# print(current_H)
# current_grad = temp_grad
# print(current_grad)
M = AbstractModel()
M.m_set = RangeSet(1, len(curr_xk))
M.n_set = RangeSet(1, 1)
M.a_set = RangeSet(1)
dic_xk = convert_xk(curr_xk)
print("xk" +str(dic_xk))
#print(dic_xk)
# M.H = Param( M.m_set, M.m_set, initialize=hessian_init)
# M.g = Param( M.m_set, initialize=c_param_init)
# M.xk = Param( M.m_set, initialize=x_param_init)
M.H = Param(M.m_set,M.m_set,initialize=current_H)
M.H_shift = Param(M.m_set,M.m_set,mutable=True)
M.g_shift = Param(M.m_set,mutable=True)
#print(current_grad)
M.g = Param(M.m_set,initialize=current_grad)
M.xk = Param(M.m_set,initialize=dic_xk)
#print(eps)
M.epsilon = Param(M.n_set, initialize={1:eps})
#M.x = Var( M.m_set, within=Binary,initialize=x_param_init)
ones_init ={}
for i in range(1,len(curr_xk)+1):
ones_init[i]=1.0
M.v = Var( M.m_set, within=Binary, initialize=ones_init)
M.pk = Var( M.m_set, domain=Reals, initialize=ones_init)
#M.alpha = Var(M.a_set,domain=NonNegativeReals,initialize={1:1.0})
M.alpha = Var(M.a_set,bounds=(.001, 1.0),initialize={1:1.0})
M.i =RangeSet(len(curr_xk))
sufficentprogress_rule = lambda M: sufficentprogress(M) <=0.0
M.de_constraint2= Constraint(rule=sufficentprogress_rule)
M.Co1 = Constraint(M.i, rule = build_line_constraint)
M.obj = Objective( rule=line_obj, sense=minimize )
instance = M.create_instance()
#print(instance.pprint())
#results=SolverFactory('mindtpy').solve(instance, mip_solver='cplex', nlp_solver='ipopt',tee=True)
#results=SolverFactory('mindtpy').solve(instance, mip_solver='gurobi', nlp_solver='ipopt',tee=True)
results=SolverFactory('mindtpy').solve(instance, mip_solver='glpk', nlp_solver='ipopt', tee=True)
#print(dir(results))
instance.solutions.store_to(results)
print(results)
new_xk=[]
for p in instance.m_set:
#print(instance.v[p].value)
new_xk.append(instance.v[p].value)
#print("lol")
#print(new_xk)
curr_xk = new_xk
counter=counter+1
obj_list.append(instance.obj.value())
first_obj = objective_func(init_x)
final_obj = objective_func(curr_xk)
print(first_obj)
print(final_obj)
print(instance.display())
print(curr_xk)
print(obj_list)
mult=10
run=1
eps=10**(-4)
for i in range(0,run):
size = mult*(i+1)
x_vec =[]
for i in range(0,size):
#x_vec.append(np.random.randint(0,2))
x_vec.append(1.0)
#x_vec.append(random.randint(0,2))
#init_obj = objective_func(x_vec)
init_x = x_vec
print(x_vec)
line_search(init_x,eps)
print(x_vec)

How to conditionally add variables to a Pyomo constraint based on predetermined data settings

I'm new to Pyomo.
I would like to know if there is an elegant way to code a constraint that contains variables that we may or may not want to include. The option to include them will be known at the time the model is solved and will be based on settings it reads in from the database.
A good example might be having slacks in the constraint, sometimes we want them and sometimes we don't.
I have tapped out a small demo below for a warehouse location example. In equation buildLimit I have added a slack to allow the number of warehouses to exceed the build limit [the code might contain some syntax errors I haven't run it]
# Import pyomo
from pyomo.environ import *
from pyomo.opt import SolverStatus, TerminationCondition
N = ['Harlingen', 'Memphis', 'Ashland']
M = ['NYC', 'LA', 'Chicago', 'Houston']
d = {('Harlingen', 'NYC'): 1956, \
('Harlingen', 'LA'): 1606, \
('Harlingen', 'Chicago'): 1410, \
('Harlingen', 'Houston'): 330, \
('Memphis', 'NYC'): 1096, \
('Memphis', 'LA'): 1792, \
('Memphis', 'Chicago'): 531, \
('Memphis', 'Houston'): 567, \
('Ashland', 'NYC'): 485, \
('Ashland', 'LA'): 2322, \
('Ashland', 'Chicago'): 324, \
('Ashland', 'Houston'): 1236 }
P = 2
model = ConcreteModel("warehouse location problem")
model.N = Set(dimen=1, initialize=N)
model.M = Set(dimen=1, initialize=M)
model.d = Param(model.N, model.M, within=PositiveIntegers, initialize=d)
model.P = Param(initialize=P)
model.y = Var(model.N, within=Binary)
model.x = Var(model.N, model.M, bounds=(0,1))
##########################
model.buildLimitSlack = Var(within=NonNegativeIntegers)
model.useSlacks = Param() # assume some data read will populate this at some stage before the solve
##########################
# Objective, minimise delivery costs
def obj_rule(model):
return sum(model.d[n,m] * model.x[n,m] for n in model.N for m in model.M) + 99*model.buildLimitSlack
model.obj = Objective(rule=obj_rule)
# All customer demand must be met
def demand_rule(model, m):
return sum(model.x[n,m] for n in model.N) == 1
model.demand = Constraint(model.M, rule=demand_rule)
# Can only ship from a warehouse if that warehouse is built
def supplyOnlyIfBuilt_rule(model, m, n):
return model.x[n,m] <= model.y[n]
model.supplyOnlyIfBuilt = Constraint(model.M, model.N, rule=supplyOnlyIfBuilt_rule)
##############################
#### WE WANT THE SLACK IN THIS EQUATION TO BE OPTIONAL BASED ON DATA SETTINGS
def buildLimit_rule(model):
return sum(model.y[n] for n in model.N) <= model.P + model.buildLimitSlack
model.buildLimit = Constraint(rule=buildLimit_rule)
##############################
I imagine we could have an if statement in the constraint, something like the following. But we don't want that as our model equations will likely have many such optional variables in the same constraint and I don't want to have tons of nested if statements [unless there is a nice way to do this?].
def buildLimit_rule(model):
if model.useSlacks:
return sum(model.y[n] for n in model.N) <= model.P + model.buildLimitSlack
else:
return sum(model.y[n] for n in model.N) <= model.P
model.buildLimit = Constraint(rule=buildLimit_rule)
Any advice?
Thanks in advance
(The question is quite general, so I'll propose some approaches but it's up to you to decide which ones work best in which situation.)
EDIT 1 - sparse components: for sparsity-related issues, see this question: Create a variable with sparse index in pyomo
Essentially, you should initialize your sets so that they only contain the "valid" values, and that will automatically ensure that you are only initializing the required components.
EDIT 2 - optional components: there are several ways to add optional components to the model. A simple if statement is a strategy, or you could look into BuildActions. Another option is to create Expressions (combinations of parameters and variables), and "optionally" change those instead of the constraint definition itself.
If all your constraints have that form (i.e. if 0 is the "neutral" state for your variables), what you can do is re-write them as:
def buildLimit_rule(m):
return sum(m.y[n] for n in m.N) <= m.P + m.buildLimitSlack * m.useSlacks
If you have more than one slack variable with independent on/off parameters:
def buildLimit_rule(m):
return sum(m.y[n] for n in m.N) <= m.P + m.v1 * m.use_v1 + m.v2 * m.use_v2 + ...
The most general (but less readable) approach is to index all the potential slack variables:
model.SLACKS = Set(...) # set for slack variables
model.slack = Var(model.SLACKS)
model.use_slack = Param(model.SLACKS, within=Binary)
def buildLimit_rule(m):
return sum(m.y[n] for n in m.N) <= m.P + sum(m.slack[i] * m.use_slack[i] for i in m.SLACKS)
One other way is to fix the variables you don't need:
if not m.useSlacks:
m.Slack.fix(0)
m.OtherSlack.fix(0)

Searching by primary key in PSQL and Django using SearchVector

I have the following method that perform search
def _search_parts(self, query: str, limit: int) -> List[Part]:
search_query = SearchQuery(query, config='simple')
if len(query.split(' ')) > 1:
for q in query.split(' '):
search_query |= SearchQuery('+'+q, config='simple')
search_vector = SearchVector('name', weight='B') + \
SearchVector('part_with_finish__manufacturer_product_code', weight='A') + \
SearchVector('part_with_finish__product_code', weight='A') + \
SearchVector('id', weight='A') + \ # <---- THIS LINE
SearchVector('short_name', weight='A')
search_rank = SearchRank(search_vector, search_query)
parts = Part.objects.annotate(rank=search_rank).filter(rank__gt=0.0).order_by('-rank')[:limit]
The following error appears with the SearchVector for the field id
DataError at /api/v1/search
invalid input syntax for integer: ""
LINE 1: ...| setweight(to_tsvector(COALESCE("app_part"."id", '')), 'A')...
^
How to make it work?
I can make a separate query by PK and merge results, but I wondering if it is possible to do it using a single query.

You must feed a value for placeholder tensor 'train_input' with dtype float and shape [1,50000]

I'm trying to implement Restricted Boltzmann model with the help of tensorflow. I'm providing input as an numpy array of size 1 X 50000.
Following are the placeholders:
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
pw = tf.placeholder(tf.float32, shape=[n_features,num_hidden], name='weights')
pbh_ = tf.placeholder(tf.float32, shape=[num_hidden],name='hidden_bias')
pbv_ = tf.placeholder(tf.float32, shape=[n_features],name='visible_bias')
When I try to run the code, I get following error:
Traceback (most recent call last):
File "rbm_1.py", line 245, in <module>
tr_err = train_network()
File "rbm_1.py", line 197, in train_network
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'train_input' with dtype float and shape [1,50000]
[[Node: train_input = Placeholder[dtype=DT_FLOAT, shape=[1,50000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'train_input', defined at:
File "rbm_1.py", line 244, in <module>
g = struct_network()
File "rbm_1.py", line 100, in struct_network
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
name=name)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'train_input' with dtype float and shape [1,50000]
[[Node: train_input = Placeholder[dtype=DT_FLOAT, shape=[1,50000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
I've verified that my numpy array is of dimension 1 X 50000. Have referred some older posts on stack overflow, but cannot figure the exact cause. Please help.
Complete code:
import tensorflow as tf
import numpy as np
# for taking MFCC and label input
import rnn_input_data
import sound_constants
# for displaying elapsed time
import calendar as cal
import time
import sys
import os
# Training Parameters
num_input = 198 # mfcc data input
n_features = 50000
training_data_size = 150 # determines number of files in training and testing module
testing_data_size = num_input - training_data_size
# Network Parameters
learning_rate = 0.0001 # for large training set, it can be set 0.001
num_hidden = 300 # number of hidden layers
num_classes = 28 # total alphabet classes (a-z) + extra symbols (', ' ')
epoch = 5 # number of iterations
batch_size = 1 # number of batches
gibbs_sampling_steps = 1
#shutting down debug logs
#os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
####################################################################################
mfcc_coeffs, _ = rnn_input_data.mfcc_and_text_encoding()
class DataGenerator:
def __init__(self, data_size):
self.ptr = 0
self.epochs = 0
self.data_size = data_size
def next_batch(self):
if self.ptr > self.data_size:
self.epochs += 1
self.ptr = 0
self.ptr += batch_size
return mfcc_coeffs[ self.ptr-batch_size : self.ptr]
def sample_hidden_from_visible(visible):
hprobs = tf.nn.sigmoid(tf.add(tf.matmul((visible),w), bh_))
#hstates = tf.nn.relu(tf.sign(hprobs - hrand))
return hprobs
def sample_visible_from_hidden(num_hidden, n_features):
visible_activation = tf.add(tf.matmul(num_hidden, tf.transpose(w)), bv_)
vprobs = tf.truncated_normal((1, n_features), mean=visible_activation, stddev=0.1)
return vprobs
def gibbs_sampling_step(visible, n_features):
hprobs = sample_hidden_from_visible(visible)
vprobs = sample_visible_from_hidden(hprobs, n_features)
hprobs1 = sample_hidden_from_visible(vprobs)
return hprobs, vprobs, hprobs1
w = tf.Variable(tf.truncated_normal(shape=[n_features,num_hidden], stddev=0.1), name='weights')
bh_ = tf.Variable(tf.constant(0.1, shape=[num_hidden]),name='hidden_bias')
bv_ = tf.Variable(tf.constant(0.1, shape=[n_features]),name='visible_bias')
pw = tf.placeholder(tf.float32, shape=[n_features,num_hidden], name='weights')
pbh_ = tf.placeholder(tf.float32, shape=[num_hidden],name='hidden_bias')
pbv_ = tf.placeholder(tf.float32, shape=[n_features],name='visible_bias')
#hrand = np.random.rand(num_hidden,num_hidden)
#vrand = np.random.rand(n_features, num_hidden)
def struct_network():
input_data = tf.placeholder(tf.float32, shape=[1,n_features], name="train_input")
encode = sample_hidden_from_visible(input_data)
reconstruction = sample_visible_from_hidden(encode, n_features)
hprob0,vprob,hprob1 = gibbs_sampling_step(input_data, n_features)
positive = tf.matmul(tf.transpose(input_data), hprob0)
nn_input = vprob
for step in range(gibbs_sampling_steps - 1):
hprob,vprob, hprob1 = gibbs_sampling_step(nn_input, n_features)
nn_input = vprob
negative = tf.matmul(tf.transpose(vprob), hprob1)
w_upd8 = w + (learning_rate *(positive - negative))
bh_upd8 = bh_ + learning_rate * tf.reduce_mean(hprob0 - hprob1, 0)
bv_upd8 = bv_ + learning_rate * tf.reduce_mean(n_features - vprob, 0)
h_rand = tf.nn.sigmoid(tf.add(tf.matmul(input_data,w), bh_))
v_rand = tf.nn.sigmoid(tf.add(tf.matmul(h_rand, tf.transpose(w)),bv_))
err = input_data - v_rand
err_sum = tf.reduce_mean(err)
# returning components as dictionary elements
return {'input_data' : input_data,
'reconstruction_error':err_sum,
'w_upd8':w_upd8,
'bh_upd8':bh_upd8,
'bv_upd8':bv_upd8
}
def train_network():
with tf.Session() as sess:
train_instance = DataGenerator(training_data_size)
sess.run(tf.global_variables_initializer())
step, error = 0, 0
tr_err = []
current_epoch = 0
while current_epoch < epoch:
start_time = cal.timegm(time.gmtime())
step += 1
trb = train_instance.next_batch()
n_w = np.zeros([n_features, num_hidden], np.float32)
n_vb = np.zeros([n_features], np.float32)
n_hb = np.zeros([num_hidden], np.float32)
o_w = np.zeros([n_features, num_hidden], np.float32)
o_vb = np.zeros([n_features], np.float32)
o_hb = np.zeros([num_hidden], np.float32)
print (trb[0])
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_hb = sess.run([g['bh_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_vb = sess.run([g['bv_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
feed = {g['input_data'] : np.transpose(trb[0])}
error_ = sess.run([g['reconstruction_error']], feed_dict=feed)[0]
error = error - error_
o_w = n_w
o_vb = n_vb
o_hb = n_hb
if train_instance.epochs > current_epoch:
current_epoch += 1
tr_err.append(error/ step)
step, error = 0, 0
return tr_err
g = struct_network()
tr_err = train_network()
My bad. The thing I missed was I was not passing input tensor in feed_dict. Instead of passing:
n_w = sess.run([g['w_upd8']], feed_dict={pw: o_w, pbh_: o_hb, pbv_: o_vb})
I tried passing:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
and it worked perfectly. Corrected portions are:
n_w = sess.run([g['w_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_hb = sess.run([g['bh_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})
n_vb = sess.run([g['bv_upd8']], feed_dict={input_data: input_data, pw: o_w, pbh_: o_hb, pbv_: o_vb})

RNN regression using Tensorflow?

I am currently trying to implement a RNN for regression.
I need to create a neural network capable of converting audio samples into vector of mfcc feature. I've already know what the feature for each audio samples is, so the task it self is to create a neural network that is capable of converting a list of audio samples in to the desired MFCC feature.
The second problem I am facing is that since the audio files I am sampling has different length, will the list with the audio sample also have different length, which would cause problem with the number of input I need to feed into to the neural network. I found this post on how to handle variable sequence length, and tried to incorporate into my implementation of a RNN, but seem to not be able to get a lot of errors for unexplainable reasons..
Could anyone see what is going wrong with my implementation?
Here is the code:
def length(sequence): ##Zero padding to fit the max lenght... Question whether that is a good idea.
used = tf.sign(tf.reduce_max(tf.abs(sequence), reduction_indices=2))
length = tf.reduce_sum(used, reduction_indices=1)
length = tf.cast(length, tf.int32)
return length
def cost(output, target):
# Compute cross entropy for each frame.
cross_entropy = target * tf.log(output)
cross_entropy = -tf.reduce_sum(cross_entropy, reduction_indices=2)
mask = tf.sign(tf.reduce_max(tf.abs(target), reduction_indices=2))
cross_entropy *= mask
# Average over actual sequence lengths.
cross_entropy = tf.reduce_sum(cross_entropy, reduction_indices=1)
cross_entropy /= tf.reduce_sum(mask, reduction_indices=1)
return tf.reduce_mean(cross_entropy)
def last_relevant(output):
max_length = int(output.get_shape()[1])
relevant = tf.reduce_sum(tf.mul(output, tf.expand_dims(tf.one_hot(length, max_length), -1)), 1)
return relevant
files_train_path = [dnn_train+f for f in listdir(dnn_train) if isfile(join(dnn_train, f))]
files_test_path = [dnn_test+f for f in listdir(dnn_test) if isfile(join(dnn_test, f))]
files_train_name = [f for f in listdir(dnn_train) if isfile(join(dnn_train, f))]
files_test_name = [f for f in listdir(dnn_test) if isfile(join(dnn_test, f))]
os.chdir(dnn_train)
train_name,train_data = generate_list_of_names_data(files_train_path)
train_data, train_names, train_output_data, train_class_output = load_sound_files(files_train_path,train_name,train_data)
max_length = 0 ## Used for variable sequence input
for element in train_data:
if element.size > max_length:
max_length = element.size
NUM_EXAMPLES = len(train_data)/2
test_data = train_data[NUM_EXAMPLES:]
test_output = train_output_data[NUM_EXAMPLES:]
train_data = train_data[:NUM_EXAMPLES]
train_output = train_output_data[:NUM_EXAMPLES]
print("--- %s seconds ---" % (time.time() - start_time))
#----------------------------------------------------------------------#
#----------------------------Main--------------------------------------#
### Tensorflow neural network setup
batch_size = None
sequence_length_max = max_length
input_dimension=1
data = tf.placeholder(tf.float32,[batch_size,sequence_length_max,input_dimension])
target = tf.placeholder(tf.float32,[None,14])
num_hidden = 24 ## Hidden layer
cell = tf.nn.rnn_cell.LSTMCell(num_hidden,state_is_tuple=True) ## Long short term memory
output, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32,sequence_length = length(data)) ## Creates the Rnn skeleton
last = last_relevant(output)#tf.gather(val, int(val.get_shape()[0]) - 1) ## Appedning as last
weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])]))
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]))
prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)
cross_entropy = cost(output,target)# How far am I from correct value?
optimizer = tf.train.AdamOptimizer() ## TensorflowOptimizer
minimize = optimizer.minimize(cross_entropy)
mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
error = tf.reduce_mean(tf.cast(mistakes, tf.float32))
## Training ##
init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
batch_size = 1000
no_of_batches = int(len(train_data)/batch_size)
epoch = 5000
for i in range(epoch):
ptr = 0
for j in range(no_of_batches):
inp, out = train_data[ptr:ptr+batch_size], train_output[ptr:ptr+batch_size]
ptr+=batch_size
sess.run(minimize,{data: inp, target: out})
print "Epoch - ",str(i)
incorrect = sess.run(error,{data: test_data, target: test_output})
print('Epoch {:2d} error {:3.1f}%'.format(i + 1, 100 * incorrect))
sess.close()
Error message:
Traceback (most recent call last):
File "tensorflow_test.py", line 177, in <module>
last = last_relevant(output)#tf.gather(val, int(val.get_shape()[0]) - 1) ## Appedning as last
File "tensorflow_test.py", line 132, in last_relevant
relevant = tf.reduce_sum(tf.mul(output, tf.expand_dims(tf.one_hot(length, max_length), -1)), 1)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2778, in one_hot
name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1413, in _one_hot
axis=axis, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 454, in apply_op
as_ref=input_arg.is_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 621, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 421, in make_tensor_proto
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 45, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <function length at 0x7f51a7a3ede8>
Edit:
Changing the tf.one_hot(lenght(output),max_length) gives me this error message:
Traceback (most recent call last):
File "tensorflow_test.py", line 184, in <module>
cross_entropy = cost(output,target)# How far am I from correct value?
File "tensorflow_test.py", line 121, in cost
cross_entropy = target * tf.log(output)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 754, in binary_op_wrapper
return func(x, y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 903, in _mul_dispatch
return gen_math_ops.mul(x, y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1427, in mul
result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2312, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1704, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1801, in _BroadcastShape
% (shape_x, shape_y))
ValueError: Incompatible shapes for broadcasting: (?, 14) and (?, 138915, 24)
tf.one_hot(length, ...)
here length is a function, not a tensor. Try length(something) instead.