Monday, April 2, 2012

How Fish Shoal (4): Using a Neural Network to Learn From Data

To recap, in the first post on this topic, I started by asking how we can use recorded data of fish movements in groups to learn how they interact. I stated that we can see this as inferring a function between the environment and the fish's behaviour, and in the subsequent posts we looked at how we might estimate functions using regression, arriving at the idea of a neural network as a highly flexible tool for performing non-linear regression. In this post we'll see how we can practically use neural networks (as one possible tool among many alternatives) to learn from data, and how this is actually coded in Matlab to show how few of the details we need to concern ourselves with to start doing useful inference.

I'll be referring to code that utilises the Netlab toolbox in Matlab, which you can download for free, and which you can install simply by unzipping the downloaded file and adding the directory to your Matlab path. The code I will use is specific to Netlab, but the basic method applies to using any similar toolbox.

Inference always begins by deciding which outputs (behaviours) we want to predict from which inputs (stimuli, environment). The recorded positions of the various fish over time taken from video tracking are only useful once we make this assignment. In the case of our research we looked at the relative positions and directions of each fish's neighbours in the group as the inputs, and the fish's responses of accelerations (or deceleration) and turning angle, as shown in the figure below taken from our paper.

The relative position and direction of a neighbour (yellow) from the focal fish (red)
So first we take all the recorded positions of the fish, and for every fish at every time step we calculate the following quantities:

1. The angle (theta) and distance (r) to the nearest neighbour, the second nearest neighbour, third nearest etc.

2. The direction (phi) of each neighbour relative to the focal fish

3. How much the fish accelerated (a) and turned (alpha) on the next time step

We also measure quantities associated with where the wall of the tank is relative to the fish, but I'll ignore these for now. 1 and 2 here are our inputs, the stimuli. 3 is the behaviours - what the fish did next in response to the stimuli.

So, assuming that we've tracked our fish and measured the above, lets get inferring....

Let's try seeing how the acceleration of the focal fish is related to the position of the nearest neighbour. Once you've got Netlab installed, you can build a neural network in just one line

my_nn = mlp(2, 10, 1, 'linear');


my_nn: is your neural network (remember from the last post, it can also be called a Multi-Layer Peceptron - mlp)

2: is the number of inputs we want to use. We will be using the angle and distance to the neighbour.

10: is the number of 'hidden nodes' - thats the number of nodes in the middle layer of the diagram we saw in the last post. We can change this number - more nodes make the network more flexible but harder to learn well. I find 20 tends to work ok, but always experiment! Each node will be a sigmoidal function of the inputs, but we're not going to worry about these details here.

'linear': means that the output will be a weighted sum of all the hidden nodes. The only real reason to change this is if the outputs are binary rather than continuous.

Now you have a neural network! But at the moment is doesn't do very much. It's been configured in a random state. You can try putting some numbers in and seeing what come out using mlpfwd


y = mlpfwd(my_nn, [x1, x2]);


where x1and x2 are any possible values of the angle and distance to the nearest fish you want to try, and y is the predicted acceleration. At the moment those predictions will be meaningless, as the network hasn't learnt anything.

Now comes the useful bit. Assume we have three vectors containing the data, theta is a vector of angles to the nearest fish, r is a vector of the distances to the nearest fish and a is a vector of how much the fish accelerated. Make sure these are column vectors. Then we can train network using just a few more lines of code.

options = zeros(1, 18); options(1)=1; options(14) = 100;
my_nn = netopt(my_nn, options, [theta, r], a, 'scg');


netopt is a function that trains the network, based on the data its given. It tries to find the values for all the parameters (like the 'slopes' in the last post) which will produce the best match between what actually comes out of the network when we put the inputs (position of the nearest neighbour) in, and the behaviours we tell it should come out (i.e. the measured accelerations). options is, as the name suggests a number of possible options. Here we only use 2. The first tells Matlab to show the error values as the algorithm learns, the 14th tells netopt to run 100 iterations of the learning algorithm. The learning algorithm is something called 'scaled conjugate gradients', which is the 'scg' at the end.

Now we can input any values of theta and r to the network and it should output a value of the expected acceleration that fits with the data it has already seen. That is about 90% of everything you need to know to start doing inference with a neural network today. All the diagrams and equations in the last post are nice to have in the back of your head while doing this, but essentially you can treat the neural network as a black box. You put data in, in the form of known inputs and outputs. You press a button to make the network 'learn', and then the box will tell you what output you should expect for any input you offer it.

First we show the network some known examples
..then we ask it to predict the output for other inputs
This is in fact the basis of pretty much all of machine-learning. Take a number of known examples of something, such as images of handwritten letters. Plug them into a learning algorithm (of which a neural network is but one among many) to train it. Then use the same algorithm to predict what some unknown examples are.

Now all that remains is to try inputting all the possible values of theta and r that we might be interested in. In our paper we made the further simplification that the function would be symmetric around the axis of the fish - i.e. if the fish will accelerate when a neighbour is ahead on the left, it will also do so if the neighbour is ahead on the right. So we test values of r between 0 and some maximum (like 40cm), and angles between 0 and pi (everywhere on the left of the fish). In Matlab we can make vectors of these test inputs like this:

r_test = linspace(0, 40, 100);
theta_test = linspace(0, pi, 100); %this gives us 100 values of each input


[r_grid, theta_grid] = ndgrid(r_test, theta_test); 
test_input = [r_grid(:), theta_grid(:)];
%this matches every value of r to every value of theta so we can test all pairs


test_acc = mlpfwd(my_nn, test_input); %this puts our test inputs through the network we learned


test_acc = reshape(test_acc, size(r_grid)); 
%and this makes the output accelerations into a matrix so we can visualise it


X = cos(theta_test)*r_test';
Y = sin(theta_test)*r_test';
pcolor(X, Y, test_acc);
%This visualises the output on a nice semi-circle

And so finally we get a plot showing what the network thinks the fish will do for any given position of the nearest neighbouring fish

That B is because this comes from a multipanel image, as we'll see soon
So we confirm some previously held beliefs about how interactions like this work. The focal fish accelerates to catch up with a neighbour in front. It slows down to rejoin a neighbour behind. And if a neighbour is too close (near the centre), this is reversed to move the focal fish to a more comfortable distance.

So in a few lines of code by us, and a lot of preprogrammed code by the makers of Netlab, we have done some quite sophisticated inference with a minimum of real maths. Of course, there are some complications in now getting from the 90% you already know to the 100% you need to get publication ready. You'll need to concern yourself things like multiple local minima of the squared error, cross-validation and such other things. But these are things to worry about once you've got your hands a little dirty and started actually doing some inference...none of them mean you can't start applying these techniques to your data TODAY!

In the next and probably last post on this topic I'll show how we go from learning this relatively simple function with just 2 inputs, to a more complex function accounting for the positions of many neighbours, and we'll investigate the perils of correlation and confounding.

[Again, if you want to read more about the details of any of these techniques, I recommend David Mackay's textbook, Information Theory, Inference and Learning Algorithms (free online). Netlab also contains a large number of demo scripts, of which demomlp1.m demo is similar to this post]





36 comments:


  1. Don't merely coexist; co-create! Collaborate with diverse creators from various backgrounds and perspectives. Merge your talents, styles, and audiences into a vibrant kaleidoscope of content. Celebrate the potent synergy of collaboration, witnessing your channels shine brighter together. Embracing diversity in collaboration not only expands your creative horizons but also fosters a rich tapestry of content that resonates with a broader audience. The fusion of unique voices creates a dynamic and engaging experience, elevating the collective impact of your collaborative endeavors.
    https://www.buyyoutubesubscribers.in/

    ReplyDelete
  2. In the heart of India's capital, Delhi, LASIK surgery shines as a symbol of medical innovation and accessible eye correction. Here, patients access world-class treatment from leading ophthalmologists and avante-garde laser technology in superbly equipped hospitals. The dedication to holistic patient care, from meticulous consultations to vigilant follow-ups, reinforces Delhi as a wise and worthy choice for vision correction through LASIK surgery.
    https://www.visualaidscentre.com/lasik-eye-surgery/

    ReplyDelete
  3. The notion of purchasing YouTube subscribers at a cheap cost is often marketed as a quick and economical solution for aspiring YouTubers to enhance their channel's appeal. These services claim to offer a rapid increase in subscriber numbers, ostensibly giving channels a competitive edge and an appearance of popularity. However, this low-cost investment tends to attract bots or inactive users, contributing little to no real engagement or viewership retention. Consequently, even though a channel might outwardly seem thriving, this form of growth doesn't translate to an active or loyal audience. Furthermore, resorting to these tactics risks contravening YouTube's stringent policies that actively combat inauthentic growth strategies, possibly resulting in punitive actions including channel demonetization or suspension. Real success on YouTube remains inherently tied to the creation of compelling and engaging content that naturally attracts and retains an authentic viewership.
    https://sites.google.com/view/buyytsubscribers/

    ReplyDelete
  4. Investing in YouTube views within the Indian market is a savvy approach for creators looking to quickly escalate their online presence. High view counts act as social validators, drawing in an audience intrigued by the content's popularity. It acts as a catalyst in the algorithm, often giving videos preferential placement and driving organic engagement. Importantly, it requires partnering with credible sources in India to secure genuine views that help foster long-term channel growth.
    https://www.buyyoutubeviewsindia.in/

    ReplyDelete
  5. Dive into the realm of online possibilities with Cost-Effective Web Hosting India, where price sensitivity meets technological robustness. Your website's availability is ensured with remarkable uptime, pivotal for establishing a reliable online identity, all without stretching your budget. Ideal for discerning entrepreneurs, our hosting packages deliver all the pivotal features you need at minimal expense. With a straightforward control panel and dedicated customer support any time you need it, your website management becomes a hassle-free affair. As your business aspirations soar, our flexible hosting solutions gracefully scale with you. Cost-Effective Web Hosting India is your gateway to a potent web presence, championing affordability without compromising on service quality. Let your digital dreams unfold economically, and witness your online project flourish.
    https://onohosting.com/

    ReplyDelete
  6. Experience the transformative power of our 'Buy YouTube Subscribers Cheap' service for your channel. This affordable solution boosts your subscriber count with genuine, active users who elevate your content's engagement and visibility. With an emphasis on affordability, our service is designed for creators who seek to enhance their YouTube presence without overspending. Enjoy the benefits of a more prominent channel, as a higher subscriber base leads to improved organic reach and stronger SEO results. Our process is in full compliance with YouTube's terms, safeguarding the credibility of your platform. Unlock the key to a more influential YouTube existence and embark on a journey to digital success with our cost-effective subscriber service.
    https://buyyoutubesubscribersindia.weebly.com/

    ReplyDelete
  7. Enhance your brand's online influence with our 'Buy Google Reviews Cheap India' service, specifically tailored for the Indian marketplace. By providing affordable, authentic reviews from genuine customers, your business can climb search rankings and gain invaluable visibility within the budget. Our service strictly adheres to local Indian guidelines, ensuring every review resonates with your target audience while maintaining absolute integrity. Secure a competitive edge in India's vibrant digital economy with our strategic solution that fortifies your local SEO and attracts a wider, more diverse customer base through trustworthy feedback.
    https://www.seoexpertindelhi.in/buy-google-reviews/

    ReplyDelete
  8. Stepping into the landscape of cheap web hosting in India unveils an arena where affordability meets performance. These providers cater to the budget constraints of startups and growing businesses, while offering a reliable 99.9% uptime. Through the utilization of user-friendly control panels and simple one-click app installations, they empower even those with minimal technical know-how. Round-the-clock customer support stands ready to answer any questions, ensuring that clients are never left in the dark. Enjoy fast server speeds, ample storage, and unlimited bandwidth, all designed to seamlessly support your website's growth. Secure hosting environments protect your online endeavors, and cost-efficiency remains paramount. As the digital realm expands, these Indian web hosting services are the economical bastions for those seeking online success without financial strain.
    https://hostinglelo.in/

    ReplyDelete
  9. Experience a tangible uplift in your business reputation with our premier paid Google reviews service in India. Dedicated to authentic quality, our reviews shine on your Google My Business page, driving SEO and building trust. Fast, affordable, and Google-compliant, we are the ally your business needs to conquer digital landscapes..SetToolTip up your brand's digital narrative with persuasive testimonials crafted to amplify your online authority and customer appeal. Count on us for a strategic advantage in the vast Indian market, ensuring your business stands tall amid competition.
    https://www.sandeepmehta.co.in/buy-google-reviews/

    ReplyDelete
  10. Join an esteemed healthcare system in vibrant Singapore as a staff nurse! We offer a challenging yet rewarding role in an advanced medical setting. Competitive salaries, professional development, and cutting-edge technology await you. Immerse yourself in a multicultural environment that praises diversity and compassionate care. Access robust support networks, maintaining work-life balance. Enjoy cultural familiarity, enriching your professional journey. Seize this chance to excel in nursing with a city that both innovates and cares deeply. Apply now to contribute to a tradition of excellence and warmth in patient services.
    https://dynamichealthstaff.com/nursing-jobs-in-singapore-for-indian-nurses

    ReplyDelete
  11. Amplify Your Music: Buy Spotify Streams and Boost Your Presence! In the crowded landscape of streaming platforms, visibility is key to success. Increase your track's credibility and attract more listeners by purchasing Spotify streams. With higher stream counts, your music gains momentum and rises in popularity, attracting even more organic listeners. Don't let your talent go unnoticed—invest in targeted promotion and watch your music climb the charts. With the power of Spotify streams, you can propel your career to new heights. Take control of your music's destiny and make a lasting impact in the industry. Buy Spotify streams today and pave the way for greater recognition and success. Don't wait for opportunities, create them with a strategic approach to promoting your tracks. Purchase Spotify streams and unlock your music's full potential!
    https://www.spotifyfame.com/

    ReplyDelete
  12. Level Up Your Stream: Buy Twitch Followers and Elevate Your Influence! In the competitive world of streaming, building a dedicated audience is paramount. Enhance your credibility and expand your reach by purchasing Twitch followers. Each follower not only boosts your visibility but also attracts more organic engagement to your channel. Seize control of your streaming journey today and invest in growing your Twitch community. Buying followers not only accelerates your growth but also unlocks doors to new collaborations and opportunities in the streaming realm. Don't let your talent go unnoticed—amplify your presence and leave a lasting impact. With strategic promotion, you can cultivate a loyal following and cement your status as a respected content creator. Don't wait for recognition; take charge with the power of Twitch followers.
    https://twitchviral.com/

    ReplyDelete
  13. Empower Your YouTube Presence: Buy YouTube Views in India and Flourish! In the thriving digital landscape of India, gaining visibility is essential. Enhance your video's credibility and attract more viewers by purchasing YouTube views tailored for the Indian market. With each view, you bolster your video's visibility and appeal to the diverse Indian audience. Take charge of your content's trajectory today and invest in expanding your YouTube views specifically in India. Buying views not only accelerates your growth but also positions you favorably in local algorithms, unlocking new opportunities within the Indian digital ecosystem. Don't let your content fade into obscurity—amplify its impact and engage with the vibrant Indian community. With strategic promotion, you can establish a robust presence and elevate your channel's reputation in this dynamic market. Don't wait for recognition; seize it with the influence of YouTube views in India.
    https://sites.google.com/site/buyytviewindia/

    ReplyDelete
  14. The concept of Silk Surgery in India is simply mesmerizing! It's like an elegant fusion of tradition and modernity, envisioning a surgical experience as smooth as silk. The very idea of incorporating such a luxurious touch into medical procedures adds a layer of sophistication to the realm of surgery. I'm intrigued by the possibilities – the techniques, the expertise, and the finesse that come with Silk Surgery. India's embrace of this innovative approach to enhance the surgical journey is a testament to its commitment to excellence. Here's to a future where healthcare seamlessly intertwines with luxury, creating a silk-threaded tapestry of wellbeing!
    https://medium.com/@pojagupta/silk-eye-surgery-elita-860c70c593ad/

    ReplyDelete
  15. The concept of Smile Pro Eye Treatment has my imagination soaring! It's like a symphony of innovation, blending dental and optical realms for a holistic approach to self-enhancement. The idea of addressing both smiles and eyes in one transformative treatment is simply brilliant. I'm envisioning a procedure that not only perfects your smile but also adds a sparkle to your eyes, creating a harmonious balance of facial radiance. The intrigue around the specifics of this treatment – the cutting-edge technology, the procedure details – is captivating. Cheers to a future where enhancing both smiles and eyes becomes a seamless, rejuvenating experience!
    https://smileproeyesurgery.medium.com/smile-pro-eye-surgery-in-delhi-fbafc721a9ce/

    ReplyDelete
  16. Smile Pro Flapless Eye Surgery – the mere notion sparks excitement for a vision revolution! Imagine bidding farewell to traditional eye surgery methods and embracing a flapless, futuristic approach to crystal-clear vision. It's like a sci-fi journey into precision and convenience. I'm intrigued by the specs of this groundbreaking procedure – the cutting-edge technology, the recovery process, and the promise of swift, flawless results. Here's to the innovators shaping the future of eye care, unlocking a world where perfect vision is achieved through a seamless, flapless experience. Cheers to a future with sharper sights and brighter smiles!
    https://www.linkedin.com/pulse/smile-pro-eye-surgery-delhi-romila-chaudhary-y5imc//

    ReplyDelete
  17. Celebrate success with elegance – choose custom trophy manufacturers to craft your unique symbol of achievement. Our artisans blend precision and creativity, ensuring each trophy reflects the essence of your accomplishment. Elevate your awards with personalized touches that make a lasting impression. From corporate milestones to sports victories, our custom trophy designs stand as tangible representations of excellence. Experience the luxury of tailored recognition with our dedicated custom trophy manufacturers, turning your vision into a tangible masterpiece. Trust in craftsmanship that goes beyond the ordinary, leaving a lasting legacy for those who strive for greatness.
    https://www.angelstrophies.com/

    ReplyDelete
  18. In the intricate maze of matrimonial legalities, find your guide with the best matrimonial lawyer in Delhi. With expertise and compassion, our seasoned professionals navigate complexities to ensure fair resolutions. Trust in the hands of experienced legal minds dedicated to protecting your rights and interests. As the top matrimonial lawyers in Delhi, we prioritize your peace of mind, offering tailored solutions for every unique case. Rest assured, our commitment to excellence sets us apart in the realm of family law. Choose the best, choose confidence – entrust your matrimonial matters to the finest legal minds in Delhi.
    https://bestdivorcelawyerindelhi.com/

    ReplyDelete
  19. Exciting Nurse Jobs in the UK beckon, no IELTS required! Step into a rewarding career without language barriers. Your skills are valued, and we're here to facilitate your seamless transition to the UK healthcare sector. Join a diverse team dedicated to providing exceptional care to patients. Explore endless opportunities to grow and thrive in one of the world's leading healthcare systems. Don't let language proficiency hold you back; our doors are open to passionate nurses worldwide. Embark on your UK nursing journey with confidence and make a meaningful impact without IELTS.
    https://suntechhealthcarepro.com/nursing-jobs-in-uk-without-ielts/

    ReplyDelete
  20. In the vibrant city of Mumbai, there exists a beacon of hope and care for those navigating the challenges of breast cancer. This distinguished Breast Cancer Specialist stands out for her deep expertise, compassionate care, and dedication to employing the latest in medical technology and research. Her practice is centered around a patient-first philosophy, ensuring personalized and comprehensive treatment plans tailored to meet the unique needs of each individual. With a strong focus on holistic healing, she provides not only medical interventions but also emotional and psychological support, making her clinic a haven for those seeking both treatment and understanding. Her commitment to empowering her patients with knowledge and choice underscores her role not just as a healer but as an advocate and educator in the fight against breast cancer.
    https://drnitanair.com/

    ReplyDelete
  21. In the bustling city of Ahmedabad, an Esophageal Cancer Specialist is making significant strides in the fight against this daunting illness. Esteemed for leveraging state-of-the-art technologies alongside time-tested medical practices, they deliver a care protocol that's both modern and effective. Their philosophy emphasizes a patient-centric approach, ensuring treatment plans are personalized to fit the unique circumstances and needs of each individual. Beyond the technical prowess, this specialist places a high value on emotional and psychological support, recognizing the profound impact these factors have on a patient's recovery process. They are also a fervent advocate for health education, dedicating time to teach the community about the importance of early detection and preventive measures. This specialist's work not only champions the technical aspects of cancer treatment but also the compassionate care that truly defines the healing process, making them a beacon of hope and outstanding medical care in Ahmedabad.
    https://drvirajlavingia.com/

    ReplyDelete
  22. Ready to skyrocket your YouTube channel's success? Now you can buy YouTube subscribers hassle-free with UPI! Our secure payment gateway ensures swift transactions and peace of mind. Say goodbye to complex payment processes and hello to instant growth. With just a few clicks, you can amplify your subscriber count and boost your channel's credibility. No need to worry about sharing sensitive information – UPI guarantees safe and encrypted transactions. Expand your reach and increase your influence with authentic subscribers. Don't miss out on this opportunity to take your channel to new heights. Buy YouTube subscribers with UPI today and watch your audience grow exponentially!
    https://www.deccanherald.com/brandspot/pr-spot/buy-youtube-views-subscribers-the-company-that-is-churning-successful-youtube-stories-one-after-another-1118911.html/

    ReplyDelete
  23. Looking to enhance your YouTube presence? Buy Non-Drop YouTube Subscribers from our trusted service today! Unlike others, we guarantee that your subscriber count will remain stable over time. Our authentic subscribers are real users who will engage with your content consistently. Say goodbye to fluctuating numbers and hello to steady growth. With our reliable service, you can build a loyal audience that truly values your channel. Invest in quality and watch your influence soar without worrying about sudden drops. Take the first step towards YouTube success and buy non-drop subscribers now!
    https://www.theweek.in/news/biz-tech/2022/07/07/buy-youtube-views-and-grow-your-business-in-2022.html/

    ReplyDelete
  24. Looking to grow your YouTube channel authentically? Buy Genuine YouTube Subscribers and amplify your online presence. Our service guarantees real subscribers who will engage with your content organically. Say goodbye to fake accounts and bots—our subscribers are genuine enthusiasts eager to support your channel. Boost your credibility and expand your reach with a loyal subscriber base. Invest in real growth and watch your channel thrive. With our reliable service, you can trust in sustainable audience development. Take the next step toward YouTube success and purchase genuine subscribers today!
    https://www.aninews.in/news/business/business/viral-promotions-the-company-that-has-created-stars-out-of-youtubers20230328182117/

    ReplyDelete
  25. In the competitive landscape of digital marketing in Australia, one SEO provider rises above the rest with a unique fusion of creativity and analytics. Distinguishing itself through a personalized approach, this provider ensures that each SEO strategy is meticulously tailored to meet the specific needs and goals of its clients. Their expertise spans across various sectors, leveraging the latest technological advancements to secure top rankings on search engine results pages. Commitment to transparency at every step of the process sets this agency apart, as they demystify SEO for their clients, presenting complex data in an understandable and actionable manner. This agency's dedication to excellence, combined with a deep understanding of digital trends, makes it an essential partner for businesses seeking to dominate the online space. Through strategic planning, innovative solutions, and ongoing support, they empower clients to achieve and maintain digital supremacy.
    https://digiquakesolutions.com/

    ReplyDelete