Question

Je fais un réseau de neurones et je voulais utiliser un hash_map pour maintenir les références de poids pour les neurones de sortie pour chaque neurone:

class Neuron; //forward declaration was there (sorry I forgot to show it earlier)
typedef double WEIGHT;
typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap;
class Neuron
{
private:
    NeuronWeightMap m_outputs;
    //...
public:

    Neuron();
    ~Neuron();
    //...
    WEIGHT GetWeight(const boost::shared_ptr<Neuron>& neuron) const
    {
        NeuronWeightMap::const_iterator itr = m_outputs.find(neuron);
        if( itr != m_outputs.end() )
        {
            return itr->second;
        }
        return 0.0f;
    }
};

Je me rends compte que je ne peux pas utiliser le boost :: shared_ptr comme la clé d'un stdext :: hash_map, alors ce serait une autre suggestion? Y a-t-il des contournements ou est la seule possibilité d'utiliser une autre touche ou peut-être passer à un std :: carte? Merci!

Voici les erreurs:

1>c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(61) : error C2440: 'type cast' : cannot convert from 'const boost::shared_ptr<T>' to 'size_t'
1>        with
1>        [
1>            T=Neuron
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(99) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(98) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &) const'
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(80) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(119) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT,
1>            _Tr=stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,
1>            _Alloc=std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,
1>            _Mfl=false
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(90) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=stdext::_Hmap_traits<boost::shared_ptr<Neuron>,common_ns::WEIGHT,stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,false>
1>        ]
1>        FILE_PATH_REMOVED\neuralnet.h(21) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT
1>        ]
1>NeuralNet.cpp
1>c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(61) : error C2440: 'type cast' : cannot convert from 'const boost::shared_ptr<T>' to 'size_t'
1>        with
1>        [
1>            T=Neuron
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(99) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(98) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &) const'
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(80) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(119) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT,
1>            _Tr=stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,
1>            _Alloc=std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,
1>            _Mfl=false
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(90) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=stdext::_Hmap_traits<boost::shared_ptr<Neuron>,common_ns::WEIGHT,stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,false>
1>        ]
1>        FILE_PATH_REMOVED\neuralnet.h(21) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT
1>        ]
1>Generating Code...
Était-ce utile?

La solution

Vous avez probablement Neuron déclaré dans votre fichier .H.

Ainsi, le ci-dessous pourrait fonctionner:

struct hasher {
    size_t operator()(const boost::shared_ptr<Neuron>& n) { return (size_t)n.get(); }
};

typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT,hasher> NeuronWeightMap;

Le problème est sans doute que boost:shared_ptr n'a pas de fonction de hachage par défaut (ce que l'on ressemblerait?), Vous avez juste besoin de fournir un.

Autres conseils

Vous pouvez également fournir une spécialisation de modèle pour le hachage:

#include <functional>
#include <boost/shared_ptr.hpp>

template<class T>
class std::tr1::hash<boost::shared_ptr<T>> {
public:
    size_t operator()(const boost::shared_ptr<T>& key) const {
        return (size_t)key.get();
    }
};

Fonctionne parfaitement avec unordered_set:

class Foo;
typedef boost::shared_ptr<Foo> FooPtr;
typedef std::tr1::unordered_set<FooPtr> FooSet;

Un "normal" Neuron* devrait fonctionner comme clé pour le hash_map:

typedef stdext::hash_map<Neuron*, WEIGHT> NeuronWeightMap;

Si vous voulez rechercher shared_ptr-Neurones vous pouvez utiliser shared_ptrs méthode get() pour accéder au pointeur brut:

NeuronWeightMap weights;
boost::shared_ptr<Neuron> n;

weights.find(n.get());

Puisque vous utilisez déjà Boost, pourquoi ne pas utiliser boost::unordered_map pour la table de hachage? Il compile hors de la boîte avec shared_ptr pour la clé, mais il vaut mieux vérifier comment le faire correctement ( Boost.Unordered )

Dans le code ci-dessus ce qui est l'erreur du compilateur?

Avez-vous essayé de déclarer avant le Neuron de classe?

class Neuron; // class Neuron must be forward declared to be used

typedef double WEIGHT;
typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap;


class Neuron
{
private:
    NeuronWeightMap m_outputs;
    //...
public:

    Neuron();
    ~Neuron();
    //...
};

une carte standard devrait également être bien dans ce cas, quelle est votre raison particulière d'utiliser hash_map?

Je trouve en ajoutant une fonction hash_value () Je peux utiliser une shared_ptr comme une clé dans un stdext :: hash_map. Ci-dessous je compare à l'aide boost :: unordered_map avec std :: hash_map

#include <boost/smart_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <hash_map>
#include <assert.h>

struct MyItem
{
    int i;
};
typedef boost::shared_ptr<const MyItem> MyItem_csptr;

struct MyExtra
{
    double d;
};

//Boost's unordered_map already knows how to hash shared_ptr
typedef boost::unordered_map<MyItem_csptr,MyExtra> MapMyItemToExtra;


//But for stdext::hash_map, we need to write this:
template<class T>
size_t hash_value(const boost::shared_ptr<T>& aSptr)
{
    return reinterpret_cast<size_t>( aSptr.get() );
};
typedef stdext::hash_map<MyItem_csptr,MyExtra> MapMyItemToExtra2;


template<class MAP>
void TryMapMyItemToExtra_T()
{
    MAP tMap;

    MyItem_csptr tItem1(new MyItem);
    MyItem_csptr tItem2(new MyItem);
    tMap[tItem1].d=1.0;
    tMap[tItem1].d=1.1;
    tMap[tItem2].d=2.0;

    assert( tMap[tItem1].d == 1.1 );
    assert( tMap[tItem2].d == 2.0 );
}

void TryMapMyItemToExtra()
{
    TryMapMyItemToExtra_T<MapMyItemToExtra>();
    TryMapMyItemToExtra_T<MapMyItemToExtra2>();
}

Problème avec la fonction de hachage se pose pour stdext::hash_set aussi bien. Solution de déclarer hash_value() est le meilleur puisque vous pouvez déclarer qu'une seule fois dans le principal fichier en-têtes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top