I was wondering if I could solve the problem described at https://github.com/cair/pyTsetlinMachine/issues/6 "myself".
I added a running example to the github issue.
The following error occurs
File "/mnt/c/ProjectsGit/BreastCancerDemo_pkl.py", line 42, in <module>
tm2.set_state(state)
File "/home/unix/miniconda3/lib/python3.8/site-packages/pyTsetlinMachine/tm.py", line 351, in set_state
for i in range(self.number_of_classes):
AttributeError: 'MultiClassTsetlinMachine' object has no attribute 'number_of_classes'
The class 'MultiClassTsetlinMachine' seems to have such as attribute?
https://github.com/cair/pyTsetlinMachine/blob/0c1ff1d43e1dd466ae0e41d50a4bde94bb36fedc/pyTsetlinMachine/tm.py#L396
Is it the case that the attribute 'number_of_classes' is not stored? but could be recovered by the dimension of the vector?
Any ideas? Hints?
You can see in the source code that the attribute gets set (the first time) you run fit, and does not get stored or retrieved in get_state or set_state. The code in that link is self.number_of_classes = int(np.max(Y) + 1), so I guess Y is supposed to be numerically encoded and one-dimensional. So yes, setting it yourself (not the dimension of the vector, but the number of unique values) should be enough. You could pickle that number together with the state, if that's more convenient.
That should at least be a good start, but I'm not familiar enough with the package to know if everything will work correctly from there.
Related
I am trying to run a migration script for my django app, but I keep getting TypeError: unhashable type: 'list' even though I am clearly passing in an Object:
I get:
error line 87, in buildCisc
c.exclusions.add(exclus)
line 944, in add
self._add_items(
line 1119, in _add_items
target_ids = self._get_target_ids(target_field_name, objs)
line 1059, in _get_target_ids
target_ids.add(target_id)
TypeError: unhashable type: 'list'
when I run the following code
...
for ex in exclusionList:
if len(Course.objects.filter(ID=ex)) > 0: # exclusion course already exsists
exclus = Course.objects.filter(ID=ex)
c.exclusions.add(exclus[0])
else: # exclusion course does not exsist yet so we must create it first
exclus = Course(ID=ex)
exclus.save()
c.exclusions.add(exclus) #this is line 87 causing the error
where c is a Course object create in prior code, exclusions is a many to many field from Course to itself, 'ex' is just a string.
if I try using exclus = Course.objects.create(ID=ex) instead that also gives the same error. The error seems to be saying that the exclus that I am passing in to c.exclusions.add is a list, but it is very clearly an object. I even tried switching exclus to exclus[0] to see if it somehow thought it was a list, but this gave me error: Course Object not iterable so it is saying it is of type Object, so I am very confused with error message. any ideas?
edit: I believe to have solved the issue, the exclus = Course(ID=ex) was the problem, specifically ex. For some reason in my read in phase, it got set to a list somehow and not a string like I thought it was
Since c is a Course object, it contains a list and that's the part that cannot be hashed. Try using .append() instead of .add(). The add() method is for sets and those require that the object be hashed since it needs to compare items for uniqueness.
I was studying the AdaDelta optimization algorithm so I tried to implement it in Python, but there is something wrong with my code, since I get the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'sqrt'
I did not find something about what is causing that error. According to the message, it's because of this line of code:
rms_grad = np.sqrt(self.e_grad + epsilon)
This line is similar to this equation:
RMS[g]t=√E[g^2]t+ϵ
I got the core equations of the algorithm in this article: http://ruder.io/optimizing-gradient-descent/index.html#adadelta
Just one more detail: I'm initializing the E[g^2]t matrix like this:
self.e_grad = (1 - mu)*np.square(nabla)
Where nabla is the gradient. Similar to this equation:
E[g2]t = γE[g2]t−1 + (1−γ)g2t
(the first term is equal to zero in the first iteration, just like the line of code above)
So I want to know if I'm initializing the E matrix the wrong way or I'm doing the square root inappropriately. I tried to use the pow() function but it doesn't work. If anyone could help me with this I would be very grateful, I'm trying this for weeks.
Additional details requested by andersource:
Here is the entire source code on github: https://github.com/pedrovbeltran/neural-networks-and-deep-learning/blob/experimental/modified-networks/network2_with_adadelta.py .
I think the problem is that self.e_grad_w is an ndarray of shape (2,) which further contains two additional ndarrays with 2d shapes, instead of directly containing data. This seems to be initialized in e_grad_initializer, in which nabla_w has the same structure. I didn't track where this comes from all the way back, but I believe once you fix this issue the problem will be resolved.
I am new on chemical network model. Currently I am converting a previous student python code to adapt the new version in the lab as titled.
firstly, a gas mixture from mechanism (pre defined) is defined
gas_mix = ct.import_phases(mech,['gas'])
then, I want to get the number of the species and use cantera nSpecies
nsp = gas_mix.nSpecies()
and I got the error message as
AttributeError: 'list' object has no attribute 'nSpecies'
Also I tried:
nsp = gas_mix.n_species
and it also shows
AttributeError: 'list' object has no attribute
Would you please kindly help me on this ?
Thank you and best regards,
YouBe
It looks like import_phases returns a list of objects--either a list of "gas mix" or just "gas" objects. I'm not really sure because this is very specific to the program you're working with.
Anyway, try looping over the values in the gas_mix and see if you can call the nSpecies() method or access the n_species attribute:
gas_mix = ct.import_phases(mech,['gas'])
for gm in gas_mix:
print(gm.nSpecies())
# or you can try this:
print(gm.n_species)
Maybe that will get you closer to what you want.
The function import_phases returns a list, which is useful for the case where you want to import multiple phase definitions from the same file, e.g.
mixtures = ct.import_phases(mech, ['gas1', 'gas2'])
where both mixtures[0] and mixtures[2] will then be a single phase definition. If you only want to define a single phase, it is easier to write:
gas_mix = ct.Solution(mech,'gas')
Or, if the mechanism file only contains one phase definition, just
gas_mix = ct.Solution(mech)
From here, you should be able to access the number of species as
gas_mix.n_species
Many of the details of migrating from the old to new Cantera interfaces are described in the documentation page "Migrating from the Old Python Module".
I'm having some issues with SymPy's current assumptions.
Look at this thread. One of the hints said to use the assume module (reference here).
I tried doing the following computation $\lim_{x \to \infty} \frac{\ln{x}}{x^k}$. I want to evaluate this limit for $k >0$.
So I tried this:
with assuming(k>0):
limit((log(x))/(x**k),x,oo)
I also tried this:
eval(limit((log(x))/(x**k),x,oo),k>0)
But regardless, I get this error:
NotImplementedError: Result depends on the sign of -sign(k)
In the case of
with assume(k>0):
limit((log(x))/(x**k),x,oo)
I get this error:
TypeError: 'module' object is not callable
Any ideas what I'm doing wrong?
This seems to work. The first answer in the thread that you linked says that "The assumption system of SymPy is kind of a mess right now". I'm not sure if that has changed since then.
k = Symbol('k', positive=True)
print limit((log(x))/(x**k),x,oo)
I can't say I fully understand the script, because classes go beyond me as yet. Anyway, I've downloaded the py-omegle module from here . And I don't seem to be able to get it to run.
Hoping that I don't need to post the whole class including functions, the part in particular that I'm having trouble with pertains to urllib2 - so I guess It's not too specific an issue - the line that causes all of the issues is:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
and it's not letting me:
#omegle.py
[ln33] self.connector.addheaders = [
[ln34] ('User-agent',user_agent)
[ln35] ]
# or
[ln98] self.id = self.connector.open(self.url+'start',data={}).read().strip('"')
Both return AttributeError:
AttributeError: 'tuple' object has no attribute 'addheaders'
# and further down
AttributeError: 'tuple' object has no attribute 'open'
Could someone please explain how to fix this? I'm sure it has something to do with the first line I posted. The entire source of the ONLY file in this module can be accessed here.
I think it's a case of misplaced parentheses.
The first line:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
creates a tuple consisting of
urllib2.build_opener(processor)
and
urllib2.HTTPHandler(debuglevel=1)
And then assigns this tuple to self.connector.