AtributeError:int object has no attribute name - python

Following is my code:
def cos_dist(self,net_1,net_2,sess):
#result
result=tf.div(product_norm,denom)
r=tf.cond(result>0.2,self.truef,self.falsef)
return r
def truef(self):
return 1
def falsef(self):
return 0
Here I am applying thresholding on result. If its value is greater than 0.2 then assign 1 otherwise assign 0. But I keep getting this error. Kindly tell what I am doing wrong.
Traceback:
Traceback (most recent call last):
File "f.py", line 326, in <module>
vgg = vgg16(imgs1,imgs2, 'vgg16_weights.npz', sess)
File "f.py", line 39, in __init__
self.cd=self.cos_dist(self.o1,self.o2,sess)
File "f.py", line 312, in cos_dist
r=tf.cond(result>0.2,self.truef,self.falsef)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1776, in cond
orig_res, res_t = context_t.BuildCondBranch(fn1)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1703, in BuildCondBranch
real_v = self._ProcessOutputTensor(v)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1661, in _ProcessOutputTensor
if val.name not in self._values:
AttributeError: 'int' object has no attribute 'name'

The callbacks should return tensors, not ints. Try:
one = tf.constant(1, dtype=tf.int32, name='one')
zero = tf.constant(0, dtype=tf.int32, name='zero')
and inside the class:
def truef(self):
return one
def falsef(self):
return zero

Related

Error executing FMU model with pyFMI: "pyfmi.fmi.FMUException: Failed to get the Boolean values"

I am using the code below to simulate a model.
def run_demo(with_plots=True):
traj = np.array([[start_time,2.25]])
input_object = ('input_1[1]', traj)
model = load_fmu('[pyfmimodel.fmu',log_level=7)
opts = model.simulate_options ()
opts['ncp']=266
# Simulate
res = model.simulate(options=opts, input=input_object,final_time=stop_time )
This is the error I am getting. I need help to resolve this error.
Traceback (most recent call last):
File "D:\Projects\Python\DOCKER\model_2.py", line 55, in <module>
run_demo()
File "D:\Projects\Python\DOCKER\model_2.py", line 38, in run_demo
res = model.simulate(options=opts, input=input_object,final_time=stop_time )
File "src\pyfmi\fmi.pyx", line 7519, in pyfmi.fmi.FMUModelCS2.simulate
File "src\pyfmi\fmi.pyx", line 378, in pyfmi.fmi.ModelBase._exec_simulate_algorithm
File "src\pyfmi\fmi.pyx", line 372, in pyfmi.fmi.ModelBase._exec_simulate_algorithm
File "C:\Users\tcto5k\Miniconda3\lib\site-packages\pyfmi\fmi_algorithm_drivers.py", line 984, in __init__
self.result_handler.simulation_start()
File "C:\Users\tcto5k\Miniconda3\lib\site-packages\pyfmi\common\io.py", line 2553, in simulation_start
[parameter_data, sorted_vars_real_vref, sorted_vars_int_vref, sorted_vars_bool_vref] = fmi_util.prepare_data_info(data_info, sorted_vars,
File "src\pyfmi\fmi_util.pyx", line 257, in pyfmi.fmi_util.prepare_data_info
File "src\pyfmi\fmi_util.pyx", line 337, in pyfmi.fmi_util.prepare_data_info
File "src\pyfmi\fmi.pyx", line 4377, in pyfmi.fmi.FMUModelBase2.get_boolean
pyfmi.fmi.FMUException: Failed to get the Boolean values.
This is the FMU model variable definition which accepts 1D array as input:
<ScalarVariable name="input_1[1]" valueReference="0" description="u" causality="input" variability="continuous">
<Real start="2.0"/>
</ScalarVariable>
<!-- 2 -->
<ScalarVariable name="dense_3[1]" valueReference="614" description="y (1st order)" causality="output" variability="continuous" initial="calculated">
<Real/>
</ScalarVariable>

TypeError: 'Tensor' object cannot be interpreted as an integer, tensorflow scan

import tensorflow as tf
from tensorflow.keras.layers import Input
max_seq_len=25
input_mask = Input(max_seq_len,)
input_mask = tf.scan(lambda _, x: tf.tensor_scatter_nd_update(x, [[0], [(tf.reduce_sum(x)) - 1]], [0, 0]), input_mask, initializer=tf.zeros(max_seq_len, dtype=tf.int64))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 574, in new_func
return func(*args, **kwargs)
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py", line 816, in scan_v2
name=name)
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py", line 599, in scan
infer_shape=True) for elem in elems_flat
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py", line 599, in <listcomp>
infer_shape=True) for elem in elems_flat
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 1082, in __init__
name=name)
File "/usr/local/anaconda3/envs/tfx/lib/python3.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 718, in __init__
self._tensor_array = [None for _ in range(size)]
TypeError: 'Tensor' object cannot be interpreted as an integer
tensorflow version: 2.3.1
keras version: 2.6.0
the function of this code is to set the first and the last '1' in input_mask vector to 0.
this code works when the input_mask = np.array([[1,1,1,1], [1,1,1,0]]),
tf.scan cannot support tensor with the first dimension is unknown ?
anyone knows how to implement the function or to fix this error? thanks

'NoneType' Object is not Subscriptable on deap expr func call

I'm aware that there are other questions with the same name, but they're all about indexing and I'm getting this error when trying to invoke func(r_curr). I've already checked and func is not NoneType after assignment, it is <function <lambda> at 0x000001BB1E91E510>.
def eval_symb_reg(individual, points):
global X
global y
global size
func = toolbox.compile(expr=individual)
predict = []
for i in range(X.shape[0]):
total = 0
for j in range(size):
r_curr = X[i][j*size:(j+1)*size]
result = func(r_curr)
total += result[0]
predict.append(total)
error = np.sum((np.array(predict) - y) ** 2)
return [1 / (error / X.shape[0])]
Stack trace:
Traceback (most recent call last):
File "symbreg.py", line 298, in <module>
main()
File "symbreg.py", line 265, in main
halloffame=hof, verbose=True)
File "C:\Users\<myuser>\AppData\Local\Programs\Python\Python36\lib\site-packages\deap\algorithms.py", line 151, in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
File "symbreg.py", line 198, in eval_symb_reg
result = func(r_curr)
File "<string>", line 1, in <lambda>
TypeError: 'NoneType' object is not subscriptable

Unable to indentify the issue here

Currently working on a portfolio code, and I am trying to print an array but I keep getting hit with
w = np.random.random((1000, len(symbols)))
w = (w.T / w.sum(axis=1)).T
print(w[:5])
Traceback (most recent call last):
File "<ipython-input-23-246da7acc0b7>", line 3, in <module>
print(w[:5])
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 1506, in _array_str_implementation
return array2string(a, max_line_width, precision, suppress_small, ' ', "")
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 712, in array2string
return _array2string(a, options, separator, prefix)
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 484, in wrapper
return f(self, *args, **kwargs)
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 510, in _array2string
format_function = _get_format_function(data, **options)
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 431, in _get_format_function
formatdict = _get_formatdict(data, **options)
File "C:\Users\godso\anaconda3\lib\site-packages\numpy\core\arrayprint.py", line 403, in _get_formatdict
fkeys = [k for k in formatter.keys() if formatter[k] is not None]
AttributeError: 'set' object has no attribute 'keys'
I have the symbols defined and all I am trying to do is the print the code below, note I have way more code but I didn't include it because everything worked fine up until now.
w = np.random.random((1000, len(symbols)))
w = (w.T / w.sum(axis=1)).T
print(w[:5])

How can I avoid numba error with recursion?

I have setup a function that iterates over combinations of chars to form strings.
It is recursive to itself, the recursive call looks like that:
testG(charNum - 1, arr2)
But when i call the entire function, I get this error:
>>> testSpeedGPU()
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
testSpeedGPU()
File "F:\Script Projects#\HASHFinder.py", line 90, in testSpeedGPU
testG(4, [''])
File "D:\Python\lib\site-packages\numba\cuda\dispatcher.py", line 40, in __call__
return self.compiled(*args, **kws)
File "D:\Python\lib\site-packages\numba\cuda\compiler.py", line 758, in __call__
kernel = self.specialize(*args)
File "D:\Python\lib\site-packages\numba\cuda\compiler.py", line 769, in specialize
kernel = self.compile(argtypes)
File "D:\Python\lib\site-packages\numba\cuda\compiler.py", line 784, in compile
kernel = compile_kernel(self.py_func, argtypes,
File "D:\Python\lib\site-packages\numba\core\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
TypeError: compile_kernel() got an unexpected keyword argument 'boundscheck'
Here is the function's body:
#jit(target ="cuda")
def testG(charNum, inpArray) -> null:
if charNum == 1:
arr2 = []
for s in range(len(inpArray)):
for i in range(len(alp)):
arr2.append(alp[i] + inpArray[s])
return
else:
print("more than 1")
arr2 = []
for s in range(len(inpArray)):
for i in range(len(alp)):
arr2.append(alp[i] + inpArray[s])
testG(charNum - 1, arr2)
I think it does have to do with the recursion but I really dont know.
Thanks for your help!
PS: The function works when not marked with #jit(target="cuda")

Categories