'function' object has no attribute 'layers' - python

from change_mask import change_circle
import numpy as np
def new_mask_update(re_diagnosis_model, mask_layer_activated, new_mask, times):
weight_s = [re_diagnosis_model.layers[i].get_weights() for i in mask_layer_activated]
newmask_copy = [np.abs(weight_s[i][0].copy()*new_mask[i].copy()) for i in range(len(weight_s))]
new_mask = change_circle(newmask_copy,times+1,N=10,spmax=1,spmin=0.5)
for i in range(len(mask_layer_activated)):
weight_s[i][0] *= new_mask[i]
return new_mask, weight_s
line4,'function' object has no attribute 'layers'

Related

Numba Vectorize FakeOverload

This program gives an error when it gets to the "#vectorize(['float32(float32)'], target='cuda')" function with the error:
File "/home/idf/anaconda3/envs/gpu/lib/python3.9/site-packages/numba/cuda/vectorizers.py", line 206, in _compile_core
return cudevfn, cudevfn.overloads[sig.args].signature.return_type
AttributeError: 'FakeOverload' object has no attribute 'signature'
Notice that I am not even calling the function. It seems to be an error at the interpreter stage:
import math
import numpy as np
from numba import vectorize,guvectorize,cuda
import timeit
def numpy_sqrt_x(x):
return np.sqrt(x)
def math_sqrt_x(x):
return [math.sqrt(xx) for xx in x]
#vectorize
def cpu_sqrt(x):
return math.sqrt(x)
#vectorize(['float32(float32)'], target='cuda')
def gpu_sqrt(x):
return math.sqrt(x)
if __name__ == '__main__':
x = np.arange(10)
print(x**2)
#print(np.exp(x))
#x = np.arange(int(1e2))
#print(timeit.timeit("numpy_sqrt_x(x)", globals=globals()))
#print(timeit.timeit("math_sqrt_x(x)", globals=globals()))
#print(gpu_sqrt(x))
Not sure what I am doing wrong?

Python unknown operand type - for custom class

I have a custom class in my Python code, that handles k-means clustering. The class takes some arguments to customize the clustering, however when subtracting two values from a list passed to the class, I get the following error:
Traceback (most recent call last):
File "/home/dev/PycharmProjects/KMeans/KMeansApplication.py", line 22, in <module>
application()
File "/home/dev/PycharmProjects/KMeans/KMeansApplication.py", line 16, in application
opt_num_clusters = cluster_calculator.calculate_optimum_clusters()
File "/home/dev/PycharmProjects/KMeans/ClusterCalculator.py", line 19, in calculate_optimum_clusters
self.init_opt_line()
File "/home/dev/PycharmProjects/KMeans/ClusterCalculator.py", line 33, in init_opt_line
self. m = (self.sum_squared_dist[0] - self.sum_squared_dist[1]) / (1 - self.calc_border)
TypeError: unsupported operand type(s) for -: 'KMeans' and 'KMeans'
Here is the code of my custom class:
import KMeansClusterer
from math import sqrt, fabs
from matplotlib import pyplot as plp
class ClusterCalculator:
m = 0
b = 0
sum_squared_dist = []
derivates = []
distances = []
line_coordinates = []
def __init__(self, calc_border, data):
self.calc_border = calc_border
self.data = data
def calculate_optimum_clusters(self):
self.calculate_squared_dist()
self.init_opt_line()
self.calc_distances()
self.calc_line_coordinates()
opt_clusters = self.get_optimum_clusters()
print("Evaluated", opt_clusters, "as optimum number of clusters")
return opt_clusters
def calculate_squared_dist(self):
for k in range(1, self.calc_border):
kmeans = KMeansClusterer.KMeansClusterer(k, self.data)
self.sum_squared_dist.append(kmeans.calc_custom_params(self.data, k))
def init_opt_line(self):
#here the error is thrown
self. m = (self.sum_squared_dist[0] - self.sum_squared_dist[1]) / (1 - self.calc_border)
self.b = (1 * self.sum_squared_dist[0] - self.calc_border*self.sum_squared_dist[0]) / (1 - self.calc_border)
def calc_y_value(self, x_calc):
return self.m * x_calc + self.b
def calc_line_coordinates(self):
for i in range(1, self.calc_border):
self.line_coordinates.append(self.calc_y_value(i))
def calc_distances(self):
for i in range(1, self.calc_border):
self.distances.append(sqrt(fabs(self.calc_y_value(i))))
print("For border", self.calc_border, ", calculated the following distances: \n", self.distances)
def get_optimum_clusters(self):
return self.distances.index((max(self.distances)))
def plot_results(self):
plp.plot(range(1, self.calc_border), self.sum_squared_dist, "bx-")
plp.plot(range(1, self.calc_border), self.line_coordinates, "bx-")
plp.xlabel("Number of clusters")
plp.ylabel("Sum of squared distances")
plp.show()
I append the KMeansClusterer as well, because sum_squared_dist is filled with values of there:
from sklearn.cluster import KMeans
from matplotlib import pyplot as plp
class KMeansClusterer:
def __init__(self, clusters, data):
self.clusters = clusters
self.data = data
def cluster(self):
kmeans = KMeans(n_clusters=self.cluster(), random_state=0).fit(self.data)
print("Clustered", len(kmeans.labels_), "GTINs")
for i, cluster_center in enumerate(kmeans.cluster_centers_):
plp.plot(cluster_center, label="Center {0}".format(i))
plp.legend(loc="best")
plp.show()
def calc_custom_params(self, data_frame, clusters):
kmeans = KMeans(n_clusters=clusters, random_state=0).fit(data_frame)
return kmeans
def cluster_without_plot(self):
return KMeans(n_clusters=self.cluster(), random_state=0).fit(self.data)
I cannot imagine why '-' should be unsupported, I trie to subtract two list values of type integer and 1 and a integer variable.
Python cannot automatically subtract classes. You need to implement the __sub__ method on your class for python to know how to handle subtracting these classes. You can find the full reference here https://docs.python.org/3/library/operator.html
KMeans.fit() returns a class instance, which implies calc_custom_params() returns a class instance, so your list sum_squared_dist does not contain integers, the elements are objects of the sklearn.cluster.KMeans class.

'numpy.ndarray' object has no attribute 'fitness'

I have this code for nsga3(evolutionary algorithm) but I get the error 'numpy.ndarray' object has no attribute 'fitness'.Generates reference points for NSGA-III selection. This code is based onjMetal NSGA-III implementation <https://github.com/jMetal/jMetal>_. Please help to remove this error
import copy
import random
import numpy as np
from deap import tools
class ReferencePoint(list): # A reference point exists in objective space an has a set of individuals associated with it
def __init__(self, *args):
list.__init__(self, *args)
self.associations_count = 0
self.associations = []
def generate_reference_points(num_objs, num_divisions_per_obj):
def gen_refs_recursive(work_point, num_objs, left, total, depth):
if depth == num_objs - 1:
work_point[depth] = left/total
ref = ReferencePoint(copy.deepcopy(work_point))
return [ref]
else:
res = []
for i in range(left):
work_point[depth] = i/total
res = res + gen_refs_recursive(work_point, num_objs, left-i, total, depth+1)
return res
print(gen_refs_recursive([0]*num_objs, num_objs, num_objs*num_divisions_per_obj,
num_objs*num_divisions_per_obj, 0))
def find_ideal_point(individuals):
'Finds the ideal point from a set individuals.'
current_ideal = [np.infty] * len(individuals[0].fitness.values) # Here th error is coming
for ind in individuals:
# Use wvalues to accomodate for maximization and minimization problems.
current_ideal = np.minimum(current_ideal,
np.multiply(ind.fitness.wvalues, -1))
print("Ideal POint is\n",current_ideal)
global individulas
individulas=np.random.rand(10,4)
generate_reference_points(2, 4)
find_ideal_point(individulas)
You can check how to prepare an input to find_ideal_point in this jupyter notebook. The implementation deals with records from deap.tools.Logbook which is "evolution records as a chronological list of dictionaries" not NumPy arrays.

'Mock' object has no attribute '__getitem__'

After following the example Mocking a Dictionary with MagicMock I have the following mock setup:
mock_writer = Mock()
mock_reader = Mock()
mock_format = Mock()
mock_option = Mock()
mock_load = MagicMock()
test_dict = {"Bus_No": Mock(), "Team_No": Mock()}
def getitem(name):
return test_dict[name]
mock_load.__getitem__.side_effect = getitem
mock_option.load.return_value = mock_load
mock_format.option.return_value = mock_option
mock_reader.format.return_value = mock_format
mock_reader.write.return_value = mock_writer
mock_spark = Mock()
mock_spark.read.return_value = mock_reader
Driver(mock_spark).run()
And here is the driver class:
def __init__(self, spark):
self.spark = spark
def run(self):
partition_names = ["Bus_No", "Team_No"]
df = self.spark.read\
.format("com.databricks.spark.avro")\
.option("avroSchema", schema)\
.load("{0}{1}*.avro".format(job.SourcePath, os.path.sep))
partition_columns = [df[x] for x in partition_names]
And then it returns this error:
partition_columns = [df[x] for x in partition_names]
TypeError: 'Mock' object has no attribute '__getitem__'
Turns out that because read is not a method I should not be using return_value from mock_spark.read. Here's the change:
mock_spark.read = mock_reader

component object has no attribute 'ln_solver'

I'm building a large new OpenMDAO component. When I run it, OpenMDAO crashes with AttributeError: 'myNewComponent' object has no attribute 'ln_solver' during the setup stage. What does this message mean?
import numpy as np
from openmdao.api import Group, Component, Problem, IndepVarComp, ParallelGroup
from openmdao.api import ScipyOptimizer
from openmdao.core.mpi_wrap import MPI
if MPI:
from openmdao.core.petsc_impl import PetscImpl as impl
else:
from openmdao.api import BasicImpl as impl
class WindSEComp(Component):
def __init__(self, nTurbs, rotor_diameter):
super(WindSEComp, self).__init__()
self.add_param('turbineX', val=np.ones(nTurbs), units='m', desc='x positions of turbines in original ref. frame')
self.add_output('AEP', shape=1)
def solve_nonlinear(self, params, unknowns, resids):
mx_opt = params['turbineX']
unknowns['AEP'] = np.sum(mx_opt)
def linearize(self, params, unknowns, resids):
mx_opt = params['turbineX']
J = {}
J['AEP', 'turbineX'] = 3 * mx_opt
return J
prob = Problem(impl=impl, root=WindSEComp(nTurbs=4, rotor_diameter=126.0))
#prob.driver = ScipyOptimizer()
#prob.driver.add_desvar('turbineX')
#prob.driver.add_objective('AEP')
prob.setup()
prob.run()
You're trying to use a component like a group: these are not the same. You want to do something like this:
top = Problem()
root = top.root = Group()
root.add('g', WindSEComp(nTurbs=4, rotor_diameter=126.0))
top.setup()
top.run()

Categories

Resources