How to create multiple Conv3DLSTMCell in tensorflow - python-2.7

I'm trying to create my model with several conv3d lstm cell layers:
I run the following code:
conv1, state1 = conv3d('conv1', _X, [8,112,112,1], [3,3,3], 64)
pool1 = max_pool('pool1', conv1, k=1)
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
pool2 = max_pool('pool2', conv2, k=2)
The conv3d functions:
def conv3d(myname, l_input, shape, kernel, outchan):
cell = contrib_rnn_cell.Conv3DLSTMCell(input_shape=shape, output_channels=out$
hidden = cell.zero_state(array_ops.shape(l_input)[0], dtypes.float32)
output, state = cell(l_input, hidden)
print(output.shape)
return output, state
My code runs OK for the conv1 and pool1 but for conv2 layer it shows me an error:
Traceback (most recent call last):
File "conv3dlstm.py", line 272, in <module>
run(16)
File "conv3dlstm.py", line 199, in run
biases)
File "/home/user/projects/model_conv3dlstm.py", line 47, in inference_c3d
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 190, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 696, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1297, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1093, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 431, in get_variable
return custom_getter(**custom_getter_kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 193, in _rnn_get_variable
variable = getter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 408, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 747, in _get_single_variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable conv_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
I saw the code in run_cell.py at line 2200 which is:
kernel = vs.get_variable(
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
Which is getting variable with fixed name "kernel". If I understand it correctly, it is supposed to be unique. But it means I can't create more of Conv3DLSTMCells than one? Is it a bug or am I using it incorrectly?

Related

Python-pptx line chart error

Newb here just getting into Python and ran into an issue that's beating me down. I have the following excerpt of Python code to create a PPT slide from an existing template. The layout and placeholders are correct but I can't get it to run with my data listed below (x, y_in, & y_out). Any help is greatly appreciated.
x = [datetime.datetime(2017, 8, 4, 15, 5, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 10, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 15, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 20, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>)]
y_in = [780993, 538962, 730180, 1135936]
y_out = [5631489, 6774738, 6485944, 6611580]
prs = Presentation('Network_Utilization_template_master.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[2])
placeholder = slide.placeholders[17]
chart_data = CategoryChartData()
chart_data.categories = x
chart_data.add_series(y_in)
chart_data.add_series(y_out)
graphic_frame = placeholder.insert_chart(XL_CHART_TYPE.LINE, chart_data)
chart = graphic_frame.chart
chart.has_legend = True
chart.legend.include_in_layout = True
chart.series[0-2].smooth = True
prs.save("Network_Utilization_" + today_s + ".pptx")
the compiler spits out the following:
Traceback (most recent call last):
File "/Users/jemorey/Documents/pptx-2.py", line 81, in <module>
graphic_frame = placeholder.insert_chart(XL_CHART_TYPE.LINE, chart_data)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/shapes/placeholder.py", line 291, in insert_chart
rId = self.part.add_chart_part(chart_type, chart_data)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/parts/slide.py", line 174, in add_chart_part
chart_part = ChartPart.new(chart_type, chart_data, self.package)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/parts/chart.py", line 29, in new
chart_blob = chart_data.xml_bytes(chart_type)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/data.py", line 104, in xml_bytes
return self._xml(chart_type).encode('utf-8')
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/data.py", line 128, in _xml
return ChartXmlWriter(chart_type, self).xml
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 803, in xml
'ser_xml': self._ser_xml,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 902, in _ser_xml
'tx_xml': xml_writer.tx_xml,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 191, in tx_xml
'series_name': self.name,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 121, in name
return escape(self._series.name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/sax/saxutils.py", line 32, in escape
data = data.replace("&", "&")
AttributeError: 'list' object has no attribute 'replace'
David Zemens is quite right in his comment. A series has a name, which appears as the first argument to ChartData.add_series(). The name appears in the legend next to the line color for that series and also appears as the column heading for the data for that series. Adding that in should get you to your next step.
Something like:
chart_data.add_series('MB in', y_in)
chart_data.add_series('MB out', y_out)

TypeError: 'NoneType' object is not subscriptable from call to integrate

Submitted issue https://github.com/sympy/sympy/issues/12993
Is this a bug? Why is this error generated?
>python
Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> A,B,y=symbols('A B y')
>>> integrate(-(A**2+B**2*(-y**2+1))**(1/2)/(-y**2+1),y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 1295, in integrate
risch=risch, manual=manual)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 486, in doit
conds=conds)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 919, in _eval_integral
result = manualintegrate(g, x)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1223, in manualintegrate
return _manualintegrate(integral_steps(f, var))
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 212, in _alternatives
result = rule(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 33, in conditioned_rl
return rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 176, in _rewriter
substep = integral_steps(rewritten, symbol)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 95, in switch_rl
return rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 335, in mul_rule
next_step = integral_steps(f, symbol)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 743, in trig_substitution_rule
a = match[a]
TypeError: 'NoneType' object is not subscriptable
>>>
Interesting thing is that, if I issue the same command right again, the error do not show up
>>> integrate(-(A**2+B**2*(-y**2+1))**(1/2)/(-y**2+1),y)
Integral((A**2 - B**2*y**2 + B**2)**0.5/((y - 1)*(y + 1)), y)
It only shows up first time it is used! It looks like using it first time loads something to memory and hence next time the error do not show up.
Any idea what is going on?

How to join/merge two data frames from Quandl?

Specificly, I use Python2.7. I read and print the two data frames from Quandl: 'FMAC/HPI_AK' and 'FMAC/HPI_CA' individually with no problem. I used merged = pd.merge(df1, df2, on = 'Date', how = 'outer') to merge the two data frames. But when I tried to merge the two data frames, I get a traceback saying keyerror: 'Date' where 'Date' is the attribute in the first/index column in both data frames.
import quandl
import pandas as pd
api_key = open('quandlapikey.txt', 'r').read()
df1 = quandl.get('FMAC/HPI_ak', authtoken=api_key)
df2 = quandl.get('FMAC/HPI_ca', authtoken=api_key)
print(df1.head())
print(df2.head())
merged = pd.merge(df1, df2, on = 'Date', how = 'outer')
merged.set_index('Date', inplace = True)
print(merged)
Date Value
1975-01-31 15.671711
1975-02-28 15.726897
1975-03-31 15.919058
1975-04-30 16.233030
1975-05-31 16.494823
Date Value
1975-01-31 34.447924
1975-02-28 34.958144
1975-03-31 35.480144
1975-04-30 36.024334
1975-05-31 36.617578
Traceback (most recent call last):
File "", line 1, in
runfile('/Users/hans/Desktop/sentdex/buildingdataset.py', wdir='/Users/hans/Desktop/sentdex')
File "/Users/hans/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/Users/hans/Desktop/sentdex/buildingdataset.py", line 22, in
merged = pd.merge(df1, df2, on = 'Date', how = 'outer')
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/tools/merge.py", line 61, in merge
copy=copy, indicator=indicator)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/tools/merge.py", line 543, in init
self.join_names) = self._get_merge_keys()
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/tools/merge.py", line 810, in _get_merge_keys
right_keys.append(right[rk]._values)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2059, in getitem
return self._getitem_column(key)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2066, in _getitem_column
return self._get_item_cache(key)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py", line 1386, in _get_item_cache
values = self._data.get(item)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py", line 3543, in get
loc = self.items.get_loc(item)
File "/Users/hans/anaconda2/lib/python2.7/site-packages/pandas/indexes/base.py", line 2136, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)
File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)
File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)
File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)
KeyError: 'Date'
You're getting that error because Date is an index in those DataFrames not a column.
You can instead do (tested):
merged = pd.merge(df1, df2, how='outer', left_index=True, right_index=True)

How to append rows in a python list?

I want to append a row in a python list.
Below is what I am trying,
# Create an empty array
arr=[]
values1 = [32, 748, 125, 458, 987, 361]
arr = np.append(arr, values1)
print arr
[ 32. 748. 125. 458. 987. 361.]
I want to append second row in the list, so that I will get an array like
[ [32. 748. 125. 458. 987. 361.], [42. 344. 145. 448. 187.
304.]]
I am getting error when I try to add second row
values2 = [42, 344, 145, 448, 187, 304]
arr = np.append(arr, values2)
How to do that?
Just append directly to your original list:
# Create an empty list
my_list = []
values1 = [32, 748, 125, 458, 987, 361]
my_list.append(values1)
print(my_list)
values2 = [42, 344, 145, 448, 187, 304]
my_list.append(values2)
print(my_list)
And this will be your output:
[[32, 748, 125, 458, 987, 361]]
[[32, 748, 125, 458, 987, 361], [42, 344, 145, 448, 187, 304]]
Hope that helps!

Unable to load custom initializer from the saved model, passing custom_objects is not working

I saved model and weights in Keras and then try to load them ,but it shows that Invalid initialization: my_init.How can I fix the problem?
model = Sequential()
def my_init(shape, name=None):
return initializations.normal(shape, scale=0.1, name=name)
def m6_1():
model.add(Convolution2D(32, 3, 3, init=my_init))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3, init=my_init))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256, init=my_init))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
save model and weights
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("model.h5")
load model and weights
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json,custom_objects={'my_init':my_init})
loaded_model.load_weights("model.h5")
error messageTraceback (most recent call last):
File "revised_learn_ETL6_load_model.py", line 73, in <module>
loaded_model = model_from_json(loaded_model_json,custom_objects={"my_init": my_init})
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/models.py", line 197, in model_from_json
return layer_from_config(config, custom_objects=custom_objects)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 36, in layer_from_config
return layer_class.from_config(config['config'])
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/models.py", line 1019, in from_config
layer = get_or_create_layer(first_layer)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/models.py", line 1003, in get_or_create_layer
layer = layer_from_config(layer_data)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 36, in layer_from_config
return layer_class.from_config(config['config'])
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/engine/topology.py", line 929, in from_config
return cls(**config)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 381, in __init__
self.init = initializations.get(init, dim_ordering=dim_ordering)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/initializations.py", line 107, in get
'initialization', kwargs=kwargs)
File "/home/ubuntu/.env/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 16, in get_from_module
str(identifier))
Exception: Invalid initialization: my_init