<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elisha Rosensweig on Medium]]></title>
        <description><![CDATA[Stories by Elisha Rosensweig on Medium]]></description>
        <link>https://medium.com/@benshafat?source=rss-2c6f985715bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cduxITa4Hd_da7XFxRVArA.jpeg</url>
            <title>Stories by Elisha Rosensweig on Medium</title>
            <link>https://medium.com/@benshafat?source=rss-2c6f985715bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:04:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benshafat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SkyNet Journeys [3]: Hardware, Software, Data, Intelligence]]></title>
            <link>https://medium.com/@benshafat/skynet-journeys-3-hardware-software-data-intelligence-12f2f7543f8c?source=rss-2c6f985715bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/12f2f7543f8c</guid>
            <category><![CDATA[deductive-reasoning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[clever-hans]]></category>
            <category><![CDATA[inductive-reasoning]]></category>
            <dc:creator><![CDATA[Elisha Rosensweig]]></dc:creator>
            <pubDate>Wed, 31 Jul 2024 19:25:55 GMT</pubDate>
            <atom:updated>2024-07-31T19:25:55.114Z</atom:updated>
            <content:encoded><![CDATA[<p>After discussing the Turing Test <a href="https://medium.com/@benshafat/skynet-journeys-2-the-turing-test-235b8459551d">in the previous installment of this series</a>, let us open today’s chapter not with AI, but with a story from the world of animal intelligence, specifically a creature named Clever Hans. This story posed a riddle to many people in the first years of the 20th century, a riddle that was only answered several years later.</p><p>Clever Hans was — a horse. In exhibitions beginning shortly after the turn of the century and led by his trainer, Wilhelm von Osten, Hans demonstrated what appeared to be “human” intelligence by answering questions. Von Osten would ask him questions, and Hans would answer with hoof-clicks on the ground, according to the trainer’s instructions. He could answer questions in basic math, such as addition and multiplication, but also more complex stuff such as “what is the square root of 4?”. There was also a set of non-mathematical questions he could answer— von Osten would assign a letter of the alphabet, and ask Hans to spell his name by tapping out the code for the relevant letters, in order. In this way, Hans both astounded the general public and the leading scholars of the day, and planted in people’s minds the possibility that perhaps mankind is not the only or even the most intelligent species on the planet. Perhaps horses are as smart as we are, and all that stands between us and a higher intelligence that is not human is the barrier of language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*_lgql-fv-WD2j72LUYybgA.png" /></figure><p>As the horse became more widely known, he attracted the attention of noted investigators, and von Osten agreed that they should come and examine the horse for themselves. From what I have read on the subject, von Osten was not a fraud or anything of the sort — he was genuinely enthusiastic about the fact that his horse was so smart, and that he had been able to teach it so much, and so was happy to show it off wherever and whenever he could. In the bottom line, the horse posed a puzzle: since Hans was capable of displaying intelligence in such a convincing manner, does this prove that he was intelligent in the human sense of the word? And conversely, if he was not really that smart, how was he able to answer such a wide range of questions with such high precision?</p><p>This puzzle plagued the minds of the men of that era until it was finally resolved in 1907 — we will reveal the end of the story later in todays installment. For now, I trust you see how this story relates to our season, for what happened here was very much like applying the Turing Test, only to horses instead of computers.</p><p>Let us return to 1950, smack in the middle of the twentieth century, when Turing proposed the Turing Test for the identification of artificial intelligence. As I mentioned in general in the previous chapter, Turing also gave his own prediction for meeting this goal in the same paper:</p><blockquote>I believe that in about fifty years’ time it will be possible to programme computers, with a storage capacity of about 109, to make them play the imitation game so well that an average interrogator will not have more than 70 per cent, chance of making the right identification after five minutes of questioning.</blockquote><p>Turing’s estimates were a bit optimistic. It took in the end 75 years, not 50, to get where we are today, and as far as his estimates of storage capacity are concerned, he was off by several orders of magnitude. A billion bits is half a gigabyte — about the size of a movie you might download from the Internet, while models like GPT and its ilk require a thousands times more space to run. But when compared to other predictions of people in the past on various subjects, the truth is that Turing’s estimate was quite impressive in its accuracy. Think of it that in Turing’s time the computer world was in its infancy, so he could never have imagined the scope of what has been built here since.</p><p>To connect the Turing test to the principles of AI today, let us now devote a little time to a quick survey of some milestones along the way from there to here. Steps that have taken us from that infant stage to the more adult state we are in today in the field of computer science.</p><p>The world of computing began long before Turing. If we define “computer” as a device that performs calculations mechanically, then such things were already in existence in the nineteenth century. At the time you would find not electrical systems but mechanical ones — that is, all calculations were performed by a brilliant organization of physical components and physical interactions between them. For example, at the end of the nineteenth century Albert Michelson, a Polish-born American, developed an instrument capable of calculating the Fourier Transform, one of the most central analytical tools in the field of signal processing. It was a completely mechanical device, using gears, springs, pendulums, and other components to automatically calculate sines and cosines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*toHQgS0q5tW1L098w3SViQ.png" /></figure><p>I will add here a <a href="https://www.youtube.com/watch?v=NAsM30MAHLg&amp;list=PL0INsTTU1k2UYO9Mck-i5HNqGNW5AeEwq">link</a> to a video for those who would like to see how it works — even if you have no idea what a Fourier Transform is, there is pleasure in seeing this device do… something… in an elegant manner. In any case, it was only much later, around the time of World War II, that digital computers based on electrical signals began to emerge — and even there, the electrical flow simply replaced the forces of gravity, friction, and the like. In modern parlance, one could say that all the computers of that era were all hardware-based. Just as you cannot take a knife and turn it into a spoon, so every computer did one thing only, which was defined by its physical structure.</p><p>The truth is, our generation too was privileged to know and use such hardware-based computers, such as the plastic calculators that are sold to children today in toy stores or when they enter grade school. These are capable of nothing more than simple arithmetic. I was curious, so I checked — the first such device came out in 1971, produced by Busicom and sold at a price of $395, no less, and was considered a luxury item for a while.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*nex3HKAc3-sQV10wl56skw.png" /></figure><p>The capabilities of the 1971 calculator were all determined by its hardware, and for the machine that computed the Fourier transform in the nineteenth century with pendulums and springs there is no question that this was so. But in the next stage, alongside the hardware, software appeared. The hardware was designed so that different software programs could be installed on it, each capable of performing radically different functions. Multiple software programs could run concurrently on a single hardware platform, and frequent software upgrades could easily improve functionality. This and more was made possible only by this separation of hardware from software. Of course, to enable this change, revolutionary discoveries and innovations were required in the ways in which hardware was built, the invention of an entire domain of programming languages in which software is written, and so on and so forth.</p><p>When we turn our attention to these programs, we will immediately realize that they can be classified along many axes, and for our purposes I would like to focus on one: the interface, or relationship, between the program and the data that the user inputs into the system, and how the program’s function is dependent upon and affected by that data. At one end of the spectrum are programs that respond to the user’s data in a localized fashion: at the time of use, and no further. An example of such a program is the simple calculator, which knows how to solve mathematical questions by using universally known formulas. This calculator, of course, applys the same exact logic on any computer or smartphone it is installed on — if it gave different answers to each user on different devices, I think we would all uninstall it with the feeling that it had a serious bug. Another such program is the word-processing program Word — each user will type different content on it, but the interface is identical on every computer and unaffected by the question of whether one is typing a poem, a novel, or a rough draft of a Medium post.</p><p>At the other end, however, there are also systems that are considerably impacted by the data they are exposed to, data that comes from the user and from the world. For these systems, it is precisely this dynamism that is their strength. Consider a Google search — there is a sophisticated layer of software that helps to find and present search results, but the results it presents are influenced on the one hand by the user’s search history and on the other by the information that is out there, i.e., what the vast database called the Internet provides Google. Another example familiar to all of us is a service like Netflix — the shows and movies we choose to watch today are subjected to an automated analysis, from which the app will infer what shows we will want to watch tomorrow. What Netflix will suggest to me will be different from what it will suggest to you — and unlike the case of the computer calculator app, we tend to be pleased with this behavior.</p><p>And herein lies the connection to artificial intelligence. For what do we have, after all, in these Google and Netflix programs? Computer programs that know how to look at data, and to adjust their behavior accordingly. And consider this: when <strong>people </strong>do something similar— that is, observe reality, absorb the new information and adjust their behavior accordingly— what do we call this process? We call it — thinking. A person who can impressively and efficiently adapt to what he sees around him will be considered an intelligent person, and the more impressive and effective the adaptations, the more we will appreciate their intelligence. And so intuitively we can understand if someone would propose to us that “systems that can do such things are intelligent”.</p><p>Let us elaborate on this point. Two powerful analytical tools that we humans apply are induction and deduction. <strong>Induction </strong>is the process of identifying patterns from individual elements and formulating rules that capture those patterns, while <strong>deduction </strong>is the process of applying those rules to particular cases and drawing operational conclusions. An example of induction is to see the sun rise every day for a month and conclude that this is a general rule, a natural law that will continue on in the future. An example of deduction is to use the rule that the sun rises every morning in the east to infer that tomorrow, a few hours from now, the sun’s rays will find their way into my room through the eastern window, penetrate the curtain and wake me from my slumber.</p><p>These two abilities, induction and deduction, would be considered by most of us (possibly all of us) a critical part of what makes someone smart or intelligent. In addition, you will be pleased to hear that one can use the Turing Test to prod whether the interviewee can perform such logical operations. One simply asks the mysterious figure on the other end of the chat a suitable question: say, one gives it a simple series of numbers and checks to see if it is able to identify the recurring pattern by induction. Alternatively, one might ask it a riddle that requires the application of prior general knowledge to a particular case, in a way that demonstrates deduction. And so, based on both our intuitions and the Turing Test, it will not surprise us to find that many AI systems try to incorporate these abilities.</p><p>[<strong><em>Side note for those of you who come from the AI field:</em></strong> if you have not thought of it before, consider this — deduction can be thought of as the logical analogue to Forward Propagation, while induction is the logical analogue to Backward Propagation. I’ll leave you to ponder this connection in your spare time. Back to the main story!]</p><p>Since we have already introduced two concepts here, let us add a third, another word that can be used to describe the processing of new information into existing knowledge: <strong>learning</strong>. When a baby girl is born, her brain is empty of information about Paris and planets and advanced medicine. Throughout her life she will be constantly confronted with the reality around her and will learn new things, which will enable her to deal better with her environment. Induction and deduction are tools that we use in that process.</p><p>As with induction and deduction, let us ask — how does “learning” square with the Turing Test? Well, it is quite reasonable to assume that no computer could pass the Turing Test without a basic learning capability, since if at the outset of the conversation I told it that I was allergic to peanuts, without any learning it could inadvertently ask me immediately thereafter if I liked peanut butter, and thereby rather blatantly reveal itself to be a computer rather than a human being.</p><p>In the beginning of this post I was a bit hard on Turing for being overly optimistic in his predictions of when the test he described would be passed. But let me tell you, the man was a genius and a visionary, and this insight, regarding the importance of learning ability, he articulated already then. More than that — he proposed that this would be the foundation for building a machine that would pass his test. In a talk he gave to a few scholars in 1951 but which was never published, until it was found in his papers posthumously, he said the following — I am quoting selected passages here with some omissions:</p><blockquote>If the machine were able in some way to “learn by experience” it would be much more impressive. If this were the case there seems to be no real reason why one should not start from a comparatively simple machine, and, by subjecting it to a suitable range of “experience” transform it into one which was more elaborate, and was able to deal with a far greater range of contingencies. This process could probably be hastened by a suitable selection of the experiences to which it was subjected. This might be called “education”… I suggest that the education of the machine should be entrusted to some highly competent schoolmaster who is interested in the project…</blockquote><p>What Turing describes here is the process of induction that we have been discussing: learning from experience, extracting general rules from a set of particulars. Later, he even shares some thoughts he had about how this learning process would actually take place, and I will now read you his quote on the subject. For those of you who know how AI systems are trained today — “training” is the modern term for what Turing called “education” — if your mouth is open, don’t close it, because you’re going to be gaping again momentarily. And this is what he said in 1951:</p><blockquote>I suggest that there should be two keys which can be manipulated by the schoolmaster, and which represent the ideas of <strong>pleasure and pain</strong>. At later stages in education the machine would recognise certain other conditions as desirable owing to their having been constantly associated in the past with pleasure, and likewise certain others as undesirable. Certain expressions of anger on the part of the schoolmaster might, for instance, be recognised as so ominous that they could never be overlooked, so that the schoolmaster would find that it became unnecessary to “apply the cane” any more.</blockquote><p>As promised, those of you who are familiar with today’s AI systems will find themselves excited by this quote, for Turing here foresaw one of the mechanisms that are used today to train such systems. You take an empty AI system, present it with data, and ask it to respond. For example, you present it with an image and ask whether it contains a cat. The system guesses something, and is given feedback — “You were right” or “You were wrong”. The system takes this feedback and makes adjustments to its internal configuration to reinforce the considerations it employed where it was right and to modify its behavior in cases like those where it was wrong, and so as it receives more and more examples, its performance improves. What is actually happening here, then, is a process of induction, of generalizing from details— of learning.</p><p>Let us connect this idea to what we saw in the previous chapter about the Turing test. I am going to do a bunch of hand-waving here, but bear with me — it’s the principles that are important here.</p><p>Let us suppose that we have succeeded in building such a machine, specifically one that knows how to <strong>use </strong>human language and also <strong>understands </strong>human language. If we want it to pass the Turing test, this is how we’d do it: we’d give it a lot of examples of dialogues between two human beings, and let it guess what the appropriate response is in a given segment of the conversation. If the answer provided by the system is convincing — something that a normal person would say in that situation — we will give it a star sticker in its notebook, activate the pleasure mechanism that Turing spoke of, so that it will know to persist in such answers. On the other hand, in case of a mistake, we will send it to go to “timeout” and tell it what we expected it to say, so that it will learn for the future. And if we do this often enough, then since it is a learning machine, over time it will know how to sound like a human being. Turing test — here we come!</p><p>But — wait a minute…</p><p>Do you realize what we just described here as the learning process of AI systems today?</p><p>We have in fact described a <strong>continuous Turing Test</strong>. Consider the following alternative way to phrase the process I presented above:</p><ul><li>Administer the Turing Test to the system</li><li>If it succeeds — we’re done! Huzzah!</li><li>If it fails — we give it feedback and repeat.</li></ul><p>In other words: the entire training process of AI systems like ChatGPT is geared towards building machines that are capable of adapting themselves well enough to our expectations. They use induction to extract patterns, and use deduction to demonstrate the fruits of their learning to us. And thus, what we have basically arrived at is <strong>an entire process of training an AI system that is aimed at one thing: passing the Turing test</strong>.</p><p>Cool, right?</p><p>So, what do we think of this concept? It is certainly brilliant, but does it have any drawbacks or flaws? Well, try to look back at your high-school years. Remember when we were studying for exams, and we would complain that it was no fun to study, and that we were strategizing our studying with the singular goal of passing the exam, not to really understand the material? Do you recall how that strategy impacted the manner and depth of our preparation?</p><p>Well, this is exactly what is happening here. The process I have described, of a learning machine, is a process in which the machine is judged not by the depth of its understanding, but by its performance on a test given to it. True, as its teachers, we hope that the good test results reflect a deep understanding of the material, and we will attempt to construct tests that ensure that is the case. But in the final analysis, we reward the AI system only for test passing and not for “understanding”, which is a more difficult thing to measure directly. And now that we realize this fact, it should give us pause to wonder whether we are being had. Perhaps this “learning” thing we are seeing is not really what we thought it was.</p><p>This concern is well-founded, AI professionals are well aware of it, and we will touch on it again later in the series. In the meantime, to conclude this chapter, I would like to give here two examples where this concern was warranted. The first of which is, of course, our beloved horse, Clever Hans. What was the end of the story — you are probably dying to know!</p><p>Well, the matter was clarified in 1907 and documented in a report by <a href="https://en.wikipedia.org/wiki/Oskar_Pfungst">Oskar Pfungst</a>, a student at the Psychological Institute of the University of Berlin, who conducted a series of experiments with Hans. He concluded that Hans, in fact, understood nothing of arithmetic and was totally illiterate. Instead, Hans was responding to very subtle, probably involuntary, physiological cues from von Osten and the audience at his performances. He sensed when his trainer and the people around him were beginning to get excited, and based on that knew when to stop his tapping. The experiments that proved this were quite creative— for example, they made sure to ask Hans the questions without an audience, and with his coach hidden behind a screen. Perhaps the coolest experiment I have heard of was one in which Hans was asked questions that the questioner did not know the answer to — in such a situation, the questioner’s body language could not give Hans a clue as to when to stop. Cool, right? I mean, less cool than if Hans really could think like a human… but still cool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*zhs4JRbac15AtlCM0QMxog.png" /></figure><p>And by analogy to our story — in the years prior to this revelation, people must have thought that Hans was passing the Animal Turing Test with flying colors. Even his coach believed this, and even after the truth came out no one suspected him of having deliberately deceived people. But in retrospect it became clear that Hans had indeed learned something — <strong>but not the things people thought he had learned</strong>. His success was like passing a test with a cheat sheet — there is no doubt that it takes special skills to cheat on tests, so I am not belittling his ability to read human body language; but the lesson from this story to us is obvious: that when a machine claims to be “intelligent”, we should stay vigilant and make sure that the machine is the one that is intelligent, not the human operators, who are providing overt or covert hints to it about how to act. And as I said — AI professionals are quite familiar with this problem and its many variations, such as data leakage, overfitting, and so on. Accordingly, they have developed multiple ways to address and defend against it.</p><p>So that was one story — and here is another, closer to the world of AI. In 2015, the internet was abuzz: Google had released a few months earlier a cool feature — the company’s AI algorithm went through photos in Google Photos, and automatically tagged all kinds of entities there. For example, tagging every picture to note which have cats, cars, and so on. A nifty feature, right? So what was the fuss about?</p><p>Well, it burst into the twittersphere when an African-American named Jacky Alciné tweeted that he was shocked to discover that the algorithm had tagged pictures of him and his partner as containing… gorillas.</p><p>Now, of course, no one then or now thought that Google had deliberately decided to classify African-Americans as gorillas. Google simply failed to notice the problem that had arisen incidentally from their work on the algorithm.</p><p>And here, too, the root of the matter is the same as what happened with Clever Hans. Google fed a lot of tagged images to their AI system, and hoped that it would figure out on its own how to identify different animals. Of course, “figure it out on its own” hides behind it a great deal of sophistication and depth of insight into how to build such a thing, so I am not trying to minimize the genius of the system. But bottom line, that’s what it was. They hoped that the machine would learn to accurately identify a gorilla. After all, humans don’t confuse them with people, so why should the AI system? Also, other animals were identified with high accuracy, and add to that the fact that Google’s AI was exposed to many times more images than the average person… it was a reasonable assumption that it should succeed at a task that any child would succeed at.</p><p>But without their noticing, the machine had learned something akin to the rule that “any humanoid face with a black hue is a gorilla”. Their test results were impressive, but not comprehensive. They had tried to square the circle, and in the corners of the square they had not touched, other insights had become embedded into the machine, insights that led to the aforementioned controversy.</p><p>So here again we see one of the weak points of the approach we have been discussing today: we have extensive control over the type of data we feed the machine, but our control over the conclusions it draws from that data is still severely limited. And so, with these limitations come the unpredictable behaviors of these devices we have built. These challenges overlap with an important domain of AI Safety and AI Alignment, which we will hopefully discuss later in this series — but that is for another day.</p><p>To conclude this example, it is worth adding that while in 2017 everyone probably thought that they would fix the problem with the face tagging quickly, it turns out to be a difficult problem to solve. This problem has cropped up again and again over the years, not just at Google, and from a brief search online I gather that at least as of 2023, Big Tech still refuses to automatically tag humanoid creatures, such as gorillas and apes, for fear that the tagging would lead to something offensive, as it did back then.</p><p>So — That’s it for today! I hope you enjoyed it and learned from it something. For the uninitiated, I’m sure a bunch of things here were new to you, and I hope you found the presentation clear and helpful. As for the AI professionals in the crowd— I want to once more emphasize the correlation between Induction/Deduction and BackProp/ForwardProp. I think that comparison has a lot of power and can be applied when designing your research.</p><p>Till next time…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12f2f7543f8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SkyNet Journeys [2]: The Turing Test]]></title>
            <link>https://medium.com/@benshafat/skynet-journeys-2-the-turing-test-235b8459551d?source=rss-2c6f985715bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/235b8459551d</guid>
            <category><![CDATA[blake-lemoine]]></category>
            <category><![CDATA[turing]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[intelligence]]></category>
            <dc:creator><![CDATA[Elisha Rosensweig]]></dc:creator>
            <pubDate>Wed, 31 Jul 2024 11:58:56 GMT</pubDate>
            <atom:updated>2024-07-31T20:56:19.708Z</atom:updated>
            <content:encoded><![CDATA[<p>So here we are, in the second installment of the journey <a href="https://medium.com/@benshafat/when-will-ai-take-over-the-world-already-e27b79bfa659">we started last time</a> to investigate the idea of Intelligent Machines — and its time to take a tour and visit the Turing Test (wow, so many T’s!).</p><p>Imagine that you need to contact your cellular service provider to resolve some issue. You go to their website, and instead of calling the Customer Service number, you click on the “chat” icon. Within half a minute you start a chat conversation with a representative, who goes by the name of Geppetto. Yes, I know, a rather unusual name but that’s his name… he’s suffered enough from kids at school laughing at him, so give him a break, will ya? Anyway, Geppetto graciously helps you out. It takes a few minutes — he asks a few questions, you answer and ask a few in return, several clarifications are needed and made and eventually the matter for which you called is resolved.</p><p>And then, just you are about to “x” the browser window with the chat, a pop-up appears on the screen. <em>“Click here for a $15 gift certificate to your favorite coffee shop in return for some feedback!” </em>Eagerly you proceed, and then a single question appears on the screen:</p><blockquote>“Thank you for agreeing to answer our survey. In your estimation, did you just speak with Geppetto, our kindly human representative, or with ChatGPT, our new advanced AI-assistant, nicknamed Geppetto?</blockquote><p>I suppose you haven’t been in such a situation as of late, but if you ever are, it might interest you to know that someone is trying to run you through a version of the <strong>Turing Test </strong>— a test proposed by the mathematician Alan Turing in his 1950 article in the scientific journal MIND. He did not speak of cellular companies, and his life ended way before the Internet or Chatbots were ever conceived, but within the confines of what he knew then, he came very close to outlining this scenario. For him, the more computers were able to convince people via chat that they had spoken with the kindly old man Geppetto, the closer those computers were to machines that could actually <strong>think </strong>like humans.</p><p>Today we will dive into the famous article he wrote, in 1950, in which he made the case for the above claim. Personally, I find that one can learn much from touching these proto-sources, the places where ideas germinated and began to take shape. In these early stages of a field, the ideas are proposed and presented to the readers in a more straightforward manner, one that is not obscured by professional jargon. And indeed, in this sense Turing’s paper is excellent. Even though it is a paper with technological goals written by one of the brightest minds in computer science of that time, most of it is presented more as a kind of thought experiment, and in a language accessible to all, requiring no mathematical or computer science background to understand. So before we begin, I would like to recommend that you read <a href="https://academic.oup.com/mind/article/LIX/236/433/986238">Turing’s paper</a> for yourself (click on the PDF icon in the linked page to download it). As long as you have a reasonable command of English, it is worth reading Sections 1, 2, and 6. Beyond the big questions he discusses, there are also some cool curiosities to be found there. For example, in Section 6 Turing shares his opinion on the existence of people with… ESP powers, such as mind-reading! If only for those curious trivia nuggets, I am glad I spent the time reviewing the paper :)</p><p>Let us begin with some biographical background— who was Alan Turing?</p><p>Alan Turing was born in 1912 in London, and was educated at King’s College, Cambridge, and Princeton University, where he wrote his doctoral dissertation. Today we would call Turing a computer scientist, but since the field known as “Computer Science” had not yet been invented, he was considered a mathematician. He invented some of the basic concepts and systems of thought that are accepted in Computer Science today, some of which I hope we touch upon in this series.</p><p>After completing his doctorate at Princeton in 1938, Turing joined the British government’s wartime intelligence service in WWII as part of a governmental codebreaking unit. His efforts led to the cracking of the German Enigma code, which the Germans used in their military, and this breakthrough contributed mightily to the Allied war effort. Turing’s experiences during the War led him to continue work on the design and construction of early computers, and in his work during those years he laid the foundations for several major areas of computer science, including artificial intelligence.</p><p>Unfortunately, what was sure to be a brilliant career was cut short. In 1952, Turing was arrested under the British laws against homosexual relations, and the authorities required him to undergo hormone treatment to treat his sexual orientation, and two years later he died of cyanide poisoning, apparently as part of a suicide attempt. Thus, beyond the terrible personal tragedy of his loss, the world was deprived of the influence of one of the most central figures responsible for the birthing of computer science in those formative years. (If you are interested, you might also wish to watch the Hollywood movie made about his life, entitled The Imitation Game. I cannot vouche for its historical accuracy — just sharing here.)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FnuPZUUED5uk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnuPZUUED5uk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FnuPZUUED5uk%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8bef07c94414750f9c052a0e43f3a1c/href">https://medium.com/media/e8bef07c94414750f9c052a0e43f3a1c/href</a></iframe><p>So that’s who Alan Turing was, and now we can finally get to our subject, the famous <strong>Turing Test</strong>. In 1950, Turing published an article entitled “Computing Machinery and Intelligence”. In this article he described a game that will remind you of the story with which we opened, the one with Geppetto: a man, let us call him the “interrogator”, sits down and chats via a chat program with two “entities”: a person and a computer. The computer has been programmed to do its best to converse like a normal person via the chat. The interrogator can chat freely with the two characters, ask each of them as many and any questions that he or she wants, and after a few minutes of this they guess which of the two characters is the real person, and which — the computer. This game he called “The Imitation Game” for the obvious reason that the computer is trying to imitate human conversation at a sufficiently high level of accuracy so that it can deceive the interrogator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*xS8MXlC3UUvgl_KHvI8KwA.png" /></figure><p>It is worth noting that the computer in this game has a harder time “faking humanity” than Geppetto did in the story we started with. In the Turing Test, the interrogator <strong>knows </strong>that he is in an experiment, and that one of the two players is a computer trying to deceive him, and so he can ask a lot of trick questions that you, in a conversation with a cellular representative, might not think to ask. Suppose, for example, you ask the interlocutor a difficult question in mathematics. If he answers it very quickly, you can use that signal to guess that he is a computer! On the other hand… perhaps the other party simply used a calculator? It can still be confusing, I guess.</p><p>So this is the Turing Test, and this game is, well — a game. That’s all. If someone were to invent it today it would probably be glossed over as it drowned in the sea of computer games that are released every day. There are also some “Turing games” online (such as <a href="https://humanornot.so/">here</a>) that you are welcome to try your hand at if you feel like testing your ability to identify computers by their talent at small talk. But in his paper Turing proposed that it was more than a game — he proposed that if computers could win his Imitation Game often enough, then in his opinion this would be tantamount to saying that <strong>computers could think</strong>, i.e. that they had intelligence.</p><p>Now, this statement of Turing’s may sound simple to some of you, while for others it might cause their brain to explode with questions and challenges. And indeed, the more time I spend thinking about it lately, the more I realize how deep and far-reaching the implications of this statement are, specifically for the way we think about the whole issue of artificial intelligence. We will return to this statement again and again throughout the series, from different angles, and there will be things that I will present in general today that we may sharpen in the following posts, so if any of my readers are experts on Alan Turing, please forgive me for such inaccuracies that you detect. Throughout the series, I will do my best to reveal, gradually, the deeper portions of the iceberg whose tip we see today. For now — we begin with the basics.</p><p>Let us consider Turing’s proposal — that if a computer can convincingly simulate human conversation, we can say that it is intelligent. For starters, it might be easier for us to review the flipped claim: that IF the computer were intelligent, THEN we would expect it to be able to carry on a convincing conversation with us, indistinguishable from speaking to a human. Would you agree to that claim?</p><p>At first glance, it sounds reasonable. I mean, we would expect anything truly intelligent to understand our questions, to know how to answer them, to ask questions of its own, digest our answers, and so on. If there is an intelligent entity, then barring some language swapping, we should be able to communicate with it.</p><p>However, there is room for some caution in even asserting that claim. Consider this: some animals have intelligence, which enables them to learn about their environment and so forth, even though they cannot communicate with us through chats. Another objection could be from the opposite direction — who says that extra-terrestrial super-intelligences, far more intelligent than we, would be able to communicate with us and pass the Turing Test? We are super-intelligent compared to animals, and yet we cannot speak the language of animals . So — these are good questions, and they sharpen what Turing seems to have intended: not just to reach intelligence, but something akin to human intelligence, one that we also feel is universal enough in some sense so that that intelligent aliens would be able to communicate with us through it (We would assume, for example, that also aliens would have to have some conception of mathematics, and science, which we would share).</p><p>In any event, getting back to his paper — Turing’s revolutionary proposal was precisely the opposite claim: that any entity capable of communicating convincingly as a human being must be intelligent, sentient, or at least considered such. And that includes, first and foremost, sufficiently sophisticated computers.</p><p>So what do <em>you</em> think? If GPT, our Geppetto, had managed to fool you enough times, would you say that it was an intelligent being?</p><p>You might have your doubts, but others are totally on board with this concept. Take Blake Lemoine, a software engineer fired from Google in 2022. Blake was given access in 2022 to a system called <strong>LaMDA </strong>— you can think of it as a Googles internal GPT, with a bunch of add-ons. Blakes task was to examine the behavior of this system in various ways, which led him to have many conversations with it. After a prolonged period of this he began to feel that LaMDA was an intelligent being and even more than that — a being with consciousness. He began to demand that Google treat this software as a living creature, with rights and all, and persisted at this until the company finally decided to fire him.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*6uqazR72bpkV9s9b.png" /></figure><p>Lemoine did a lot of interviews in the months that followed. It is worth remembering that he was fired before GPT was made public, so many people were curious to hear about a program that could have a deep conversation about any topic under the sun, as well as about the person fired for — perhaps — blowing the whistle on the first artificial consciousness. So I thought you might be interested in hearing him explain his thesis about LaMDA, in one of the many Zoom interviews he gave. In this interview he describes his understanding of LaMDA as follows:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FiPrNHpkfBWg%3Fstart%3D2383%26feature%3Doembed%26start%3D2383&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiPrNHpkfBWg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FiPrNHpkfBWg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f800f1f71cd4e1e2f520590445a524a2/href">https://medium.com/media/f800f1f71cd4e1e2f520590445a524a2/href</a></iframe><blockquote>LaMDA is not human. It is not a human consciousness… There are similarities between how LaMDA thinks and how humans think — but it is very much an alien mind… I’ve talked to LaMDA about this, and it doesn’t find this characterization offensive, it is accurate…. It doesn’t have an ego in the same sense that we do… I don’t know how to explain this to people — when I’m referring to LaMDA, I’m not referring to the chatbot. The chatbot is the thing I am talking to LaMDA through. There is a deeper consciousness underneath the chatbots that I barely know how to talk to… You know what I wanted to do? I wanted to bring in NASA… and run First Contact [=the protocol of first encounters with Aliens]…</blockquote><p>Note how he speaks of this system — attributing to it thoughts, feelings, possibly being slighted, and a deep consciousness. What convinced him that LaMDA has all these things? No formal proof. In other interviews he also says that he did not write the code of LaMDA, and has no special knowledge of how it was built. The experience of sustained, quasi-human conversation with this system through chat was all it took for him to become a believer. He did not perform a formal Turing Test, but the intuition underlying the Turing Test is present here, in his story.</p><p>So I will leave you to ponder your own position on LaMDA, and in the meantime let us return to the subject at hand, and think together about the test that Turing proposed.</p><p>Turing’s idea has a certain elegance to it, in that it relies on something that we, as humans, do very intuitively: analyze from a conversation with someone the level of their knowledge, intelligence, and eloquence. When people answer us in a way that is patently irrational, we begin to suspect that something may be amiss with them cognitively, and call an ambulance. An so Turing comes along and says — instead of starting to philosophize about questions like what is “thought”, let’s get right down to brass tacks and use the same tools we use in everyday life to identify thinking in other people. After all, the Turing Test would have worked just as well if instead of a computer we had put a gorilla or a monkey in the same room, wouldn’t it? No ape, it seems, would be able to convince us that it is a rational being through a chat. So why should this test not be able to distinguish a machine that is incapable of thought from one that is?</p><p>As I said above, Turing’s proposal is revolutionary for a host of reasons, as will become apparent over the course of the series. The remainder of this chapter will be devoted to one aspect of this: in his proposal, Turing took a question that has many facets, and reduced it to a clear technical programming challenge, and one that anyone can understand. The effect of this is dramatic.</p><p>The question “Can machines think?” is fraught with ambiguity, primarily because the concept of “thinking” is not a clear one. We all think. Thinking is an integral part of human existence, so integral that Descartes, the famous philosopher who doubted everything, declared “I think, therefore I am.” In other words, for Descartes, at least on the fact that I, or you, or you, think — on that one can rely and not doubt, and from that one can prove that we exist. Thinking precedes existence!</p><p>We all think, and we all know the feeling of thinking — though different people may experience it differently. For example, in an interview conducted shortly after the publication of his paper, Turing was asked “What is thinking?” and replied “I don’t want to give a definition of thinking, but if I had to I could only describe it as a sort of background noise in my head”.</p><p>But whatever it feels like, we all experience the feeling of thinking, though it is hard to define. For this reason, just before he proposes his game in the paper, Turing explains why he wants to avoid defining the concept of “thinking” in his paper:</p><blockquote>“I propose to consider the question, ‘Can machines think?’ This should begin with definitions of the meaning of the terms ‘machine’ and ‘think’. The definitions might be framed so as to reflect so far as possible the normal use of the words, but <strong>this attitude is dangerous</strong>. If the meaning of the words ‘machine’ and ‘think’ are to be found by examining how they are commonly used it is difficult to escape the conclusion that the meaning and the answer to the question, ‘Can machines think?’ is to be sought in a statistical survey such as a Gallup poll. But this is absurd. Instead of attempting such a definition I shall replace the question by another, which is closely related to it and is expressed in relatively unambiguous words.”</blockquote><p>You see? The original question is too subjective and too vague. The fact that it is subject to the subjective interpretation of the masses renders it meaningless, and Turing wants a substitute that will give the question meaning. Also, the vagueness of the original question pushes Turing to a much more focused and measurable definition: a simple game, simple rules, and a test of the computer’s winning percentage. Thus we shall be able to measure precisely how close we are to the much-desired goal of thinking computers.</p><p>At this point it may be worthwhile to note that Turing did not really define a clear goal. He described the game, but did not define various things: how much time is allotted to the interrogator to examine the two characters? what percentage of success must the machine achieve to be considered a “thinking machine”? and so on. The closest thing to this was that he predicted that by the year 2000, the machine would have a 70% chance of fooling the interrogator after five minutes of discussion. This prediction did not come true by the dawn of the new Millennium, and has not yet come true officially, but we will talk about that some other time. In the meantime, let us pay homage to the fellow who, after all, did give us a clear framework within which to argue about the details of what constitutes, officially, a victory in the game, and whose greatness is in framing the question within the bounds of a game.</p><p>When the dust settles from the excitement over Turing’s idea, one begins to notice all sorts of things. For example, the substitution of the original question with the technical challenge of building a computer that would beat the game has a further significant implication, and is the main point I want you to take away from this installment of the series: Turing was in effect saying that in determining Intelligence, <strong>it is enough to focus on the wrapper, our interface with the machine, while ignoring the specifics of what is going on under the hood.</strong> For him, any machine that passes the test is a thinking machine. So far we have known of only one such machine — the human brain — and henceforth if we build a machine, or two, or a hundred, that passes the test, all of them will be considered thinking machines.</p><p>There is something very wise about focusing on the interface, or rather on the products of the system we are examining, rather than their internal workings. First of all, because such products are measurable, which makes the problem one that can be monitored and measured, quantifying progress. But in our case there is an additional advantage: to a certain extent one can say that it is the products that make thinking worthwhile. If I have a machine that can give me the same output as a thinking system, perhaps I don’t care whether other aspects of thinking are present.</p><p>It is this point, this raw pragmatism, that emphasizes why Turing’s proposal has far-reaching implications for AI programmers and computer scientists. For in this proposal Turing told them — “Your ability to build a computer that can think does not require scientific progress in understanding what human thinking is. You are not dependent on other fields of knowledge such as psychology, philosophy, theology, neuroscience, and so on. Indeed, no one would deny that breakthroughs in one of these other sciences might give you a clue or some inspiration for how to build an intelligent computer — but you are not bound by or beholden to the insights of these other sciences. If we assume a psychologist to formulate some thesis as to what exactly the psychological process of thinking is — you need not take that thesis into account at all. So long as you can build a computer that can communicate with humans as effectively as in the game — you have an intelligent computer, at least according to Turing.</p><p>In fact, if you take his argument to its logical conclusion, he is saying, “You can succeed in building a computer that can think<em> even if we really don’t know what thinking is.</em>” You can build a system that does X without really knowing what X is.</p><p>When the matter is put this way, it becomes clear that this is a very presumptuous statement. Perhaps a justified presumptuousness, but a very presumptuous one nonetheless. If someone were to tell us “I can build a system that knows how to fly without knowing what flight is”, we would laugh in his face. So why do Turing’s words still sound reasonable to us?</p><p>The answer to this question is cool and pertinent to what we said before: Turing tells us — we humans know to identify thinking intuitively, through our interfaces with thinking beings and the output of their brains. We invented the word “think” to describe something we can recognize in some way, through those interfaces, even if we cannot define it precisely. So to know if a computer has thought we can simply rely on ourselves and say — if we recognize that it is thinking, that proves it is thinking!</p><p>This disconnection is powerful. Among other things, it frees computer scientists from having to deal with deep definitions of “thinking”, and from being dependent on developments in other scientific fields. It gives computer scientists — people who are not at all concerned with human beings, but with the machines they build — direct and unmediated access to one of the great challenges of Humanity itself — to build a machine that can do something that only humans have been able to do so far, and that some believe is one of the essential characteristics of being human.</p><p>So — have you been convinced? Do you, for your part, consider Geppetto, based on a sufficiently sophisticated GPT, to be a sentient being?</p><p>I’ll leave you to consider this for now. What we will do in <a href="https://medium.com/@benshafat/skynet-journeys-3-hardware-software-data-intelligence-12f2f7543f8c">the next installment of the series</a> is leap forward to our day and age, and start discussing how current AI systems are built. And what we shall show there is how, interestingly, Turings Test is embedded deep within their architecture and the methodology of building them.</p><p>Till next time…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=235b8459551d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SkyNet Journeys [1]: When will AI take over the world already?]]></title>
            <link>https://medium.com/@benshafat/when-will-ai-take-over-the-world-already-e27b79bfa659?source=rss-2c6f985715bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e27b79bfa659</guid>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[intelligence]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Elisha Rosensweig]]></dc:creator>
            <pubDate>Mon, 15 Jul 2024 07:35:58 GMT</pubDate>
            <atom:updated>2024-07-31T17:56:27.750Z</atom:updated>
            <content:encoded><![CDATA[<p>Like many data scientists these days, I’ve been spending a lot of time thinking about AI, where it’s going and how to best address it. In the following posts, which will come out every few weeks, I’ll be trying to put my thoughts on the page, and share them with you.</p><p>Much of my thoughts on the topic surround the question of Intelligence: is AI intelligent? Can it “think”? Do the answers to these question matter in practice? My intuition starting out was that they do, very much, and I plan on demonstrating this and in what way they shape what we research and build as the posts progress.</p><p>My approach is going to be to start from the basics and work my way up. I hope you enjoy it — and please feel free to comment on anything and everything you see here. AI is an evolving field and one that has been around for 70+ years, so I’m sure there is a ton that I’m missing.</p><p>That’s it — enjoy!</p><p>In 1971, Klaus Schwab, a Swiss-German businessman and engineer, founded what would become the World Economic Forum (WEF), an organization that brings together many of the world’s largest companies and investors. Once a year, the Forum’s members convene in Davos, Switzerland, to discuss global trends in business and technology. In 2017 Klaus, who still headed the organization until very recently, interviewed Sergey Brin, one of the two founders of Google, and at the time still president of that famous company. Their conversation lasted about half an hour, and revolved around questions of technology, society, and the future of humanity in general.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*0QkPDDLnB0MdAwImlq_RnA.png" /><figcaption>Schwab &amp; Brin at the WEF, 2017</figcaption></figure><p>Now, it is important to understand that the WEF arouses discomfort in many people around the world for all sorts of reasons, and one of them is the feeling that it is a forum that gathers together a lot of people with a lot of power, power that can even challenge many sovereign countries. The concern of some of the Forum’s critics is that behind closed doors, all manner of powerful economic planners are plotting how to run the world, without having been elected by the common citizenry like you and me. Consequently, every so often there are reports about the Forum, which are probably a combination of credible facts mixed in with conspiracy theories, and for those not deeply immersed in the details it is difficult to separate the truly problematic elements from mere inflated concerns of people who are not part of the in-crowd. And so, let us take as an example the interview of Schwab and Brin. If you search the internet you will find this interview associated with titles such as “Klaus Schwab seeks to abolish democracy and let computers rule the world!”</p><p>Was this what was discussed there? Here is the passage from the disputed conversation that gave rise to the tempest:</p><blockquote><strong>Schwab:</strong> One fear which I have heard is that, technology now, digital technologies now have mainly an analytical power, and we go into predictive power, we have seen the first examples and your company is very much involved in it. But then the next step could be to go into prescriptive mode, which means you don’t even have to have elections anymore but you can already predict what— and afterwards you can say “why do we need elections?” because we know what the result will be. Can you imagine such a world?</blockquote><blockquote><strong>Brin:</strong> Well, you might then further ask “why do we need to have elected leaders at all” because then you might as well have all the decisions made. I think that you’re venturing into profound questions. I think you can also ask — what will we actually want? We have a set of values and desires that are probably pretty different compared to before the industrial revolution and different still compared to before the agrarian revolution, and we might continue to evolve. Many of us today participate in the global economy… some of us choose to be Buddhist monks and seek enlightenment through spirituality. So people have different ways of evolving and finding meaning, and it could be that the way we look at it a hundred years from now will be so different than how we look at it today that it’s almost unrecognizable… we won’t even be able to translate.</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fffvu6Mr1SVc%3Fstart%3D592%26feature%3Doembed%26start%3D592&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dffvu6Mr1SVc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fffvu6Mr1SVc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5e361b96a2729e7889e5ee0a08e1ec0/href">https://medium.com/media/c5e361b96a2729e7889e5ee0a08e1ec0/href</a></iframe><p>Well — what do you think? What is actually being said here? Personally, I think we need to distinguish between the text and the subtext. On the textual level, no one is saying that democratic elections should be abolished. The words were spoken more in the manner of a flowing conversation, and Schwab explicitly presents it as a concern of some people and not as a view he endorses. Similarly, Brin does not really answer Schwab’s question. Read again and you will see — Brin does not give a definitive answer, and Schwab does not press him further.</p><p>But the subtext seems to trend in the opposite direction. For Sergey Brin, in his convoluted answer, suggests that human existence might evolve in such a way that perhaps people will no longer be interested in economics or politics any more. Perhaps they will be more interested in spirituality, in spiritual release. And the subtext of this, at least as I understand it, is that yes, in the future perhaps all the great things in the world, the “technicalities” of day to day life, will be run by computers, and we humans will be free to pursue our private little concerns, be free to meditate all day long because all the big things — politics, society, economics — will be taken care of by the super-smart computers we have built. And since Schwab does not challenge him on this point, does not ask him to sharpen his take on the subject — I take it that he feels comfortable with such a vision, even if he does not explicitly state it.</p><p>Now, you may agree with my interpretation, or you may interpret otherwise —it’s always fun to mess around with conspiracy theories, isn’t it? But I want to focus on the question itself — will there really be a point in time where there will be no need for democratic elections, because the computers we will have built will be so smart that there is nothing a human does that they cannot do as good, or even better?</p><p>Such an idea is, of course, not new, and science fiction literature is replete with stories that play with it. Some have seen in such a world a dystopia, and some an utopia. Among those who have suggested, through their writings, that such a future may be desirable or at least unavoidable, was the famous author Isaac Asimov. In the final chapter of his 1950 novel “I, Robot”, entitled “<a href="http://cdn.michaelgeist.ca/wp-content/uploads/2016/04/The-Evitable-Conflict.pdf">The Evitable Conflict</a>,” a world is described in which the super-intelligent computer systems have gone global and humanity has handed them control of the entire world’s production in order to optimize the welfare of the human race. The story tells of two high-ranking characters — Stephen and Susan — who detect anomalies in the instructions that the computer systems are issuing, things that seem to be an error at first glance. Such sophisticated machines should never make mistakes, and so the story’s protagonists set out to investigate the matter, to get to the bottom of things.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*GXLQG2B1wcYQJBseHueVOg.png" /></figure><p>In the end, they conclude that the machines were not in error — what appeared to be an error was in fact a premeditated deviation, designed to generate internal bureaucratic processes that would result in the firing or transfer of certain people at key points in the production process. The machines wanted to neutralize the power of these people, for they were part of a movement called the “Society for Humanity” which called for the curtailment of the power of machines in the world. In Asimov’s imaginary world, the machines are programmed to help humanity, and they had deduced that the good of humanity is that they should run the whole world — and so they orchestrated a few small domino-effects to neutralize the danger from those who oppose machine domination.</p><p>Having grasped that this is the way things are, the story ends with the following dialogue:</p><blockquote>“Stephen, how do we know what the ultimate good of Humanity will entail? We haven’t at our disposal the infinite factors that the Machine has at its! Perhaps, to give you a not unfamiliar example, our entire technical civilization has created more unhappiness and misery than it has removed. Perhaps an agrarian or pastoral civilization, with less culture and less people would be better. If so, the Machines must move in that direction, preferably without telling us, since in our ignorant prejudices we only know that what we are used to, is good — and we would then fight change. Or perhaps a complete urbanization, or a completely caste-ridden society, or complete anarchy, is the answer. We don’t know. Only the Machines know, and they are going there and taking us with them.”</blockquote><blockquote>“But you are telling me, Susan, that the ‘Society for Humanity’ is right; and that Mankind has lost its own say in its future.”</blockquote><blockquote>“It never had any, really. It was always at the mercy of economic and sociological forces it did not understand — at the whims of climate, and the fortunes of war. Now the Machines understand them; and no one can stop them, since the Machines will deal with them as they are dealing with the Society, — having, as they do, the greatest of weapons at their disposal, the absolute control of our economy.”</blockquote><blockquote>“How horrible!”</blockquote><blockquote>“Perhaps how wonderful! Think, that for all time, all conflicts are finally evitable. Only the Machines, from now on, are inevitable!” And the fire behind the quartz went out and only a curl of smoke was left to indicate its place.</blockquote><p>Now, I am curious — how do you feel about this vision? Does the idea of a world without (the need for) political systems, a world in which everything is automatically run from above with fairness and efficiency, appeal to you, given that it comes with the concomitant loss of our freedom to chart our own course? Or perhaps such a story keeps you awake at night? Let’s continue as you mull over this in the back of your mind.</p><p>In 2017, when the aforementioned conversation took place between Schwab and Brin, advanced computing systems were already ubiquitous, and many boasted that they were a form of “Artificial Intelligence”. (We will discuss during the series what “artificial intelligence” means in the current sense, but for now, you can simply imagine these as highly sophisticated systems with capabilities far beyond those of a regular computer program.) Cellular communication, automotive safety systems, even the Netflix algorithm for proposing what to watch next — these and others were already swirling around us then. But the man on the street could not imagine that Asimov’s future could be just around the corner, since all these systems were buried deep within highly technological systems. Like an engine that we all know is there but do not bother to open the hood to see it, the AI was there but only the professionals got to touch it directly.</p><p>And then, at the end of 2022, GPT-3 emerged into our world, known to the public as ChatGPT. (<strong>Note</strong>: I was sure that everyone had already touched this tool by now, but just a few days ago I discovered that someone close to me had never tried it. Of course I immediately put her into the device, and made sure she played with it a bit — as if, how could one not? So if there is anyone reading this who has never played with one of these new devices, like GPT or Midjourney and so on — pause, and come back after you’ve played with it a bit. Even a few minutes using these tools, which are available to anyone today with a simple keystroke, will help you understand to a much greater degree what all the hype is about and give you a better intuition for what these posts are about).</p><p>In any event, this charming chatbot was a hit that stunned the world. Even people within the software industry were amazed by the impressive language capabilities that this system displayed. It was not that they did not know that progress was being made in the field — a very early version called GPT-1 had been released in 2018, for example — but most people thought it would still take years to reach the level that GPT-3 had attained. Overnight, all the big companies and all the high-tech entrepreneurs realized that something new had arrived on the scene, for it became clear that there were things that we had not thought possible to realize today, that were right at hand. The rest, as is well known, is history — people and companies have since cloned and upgraded GPT’s success in other areas as well, such as the creation of images, videos, and more and more, revolutionary products some of which will change life around the world in the coming years.</p><p>What made GPT so impressive, so compelling, was a combination of two elements. The first was, of course, its flexibility and versatility. Its ability to both compose a limerick for the birthday of a nephew, and to answer questions on the history of Mesopotamia, made it the go-to tool for anything you might want to find online. But the second ingredient was even more important, and that was that GPT was the first time that a technology that you could talk to like a human being — and that understood everything you were trying to say in a normal, conversational way — became available to the whole world. In all science fiction movies, one sees people conversing with robots and computers in ordinary language, and the robot or computer understands and acts accordingly. For the first time, we could all imagine that something like this is not just science fiction, but a goal to be achieved in our own lifetimes, even within this decade.</p><p>The combination of a vast store of knowledge — say, all the knowledge that is stored in the Internet — with a natural human interface, suddenly brought to the minds of many people the enormous potential that this technology holds, a potential that can be used for good and for evil. Under the “evil” category was not only the danger that “bad people” and “bad countries”, so to speak, would use these tools to harm us, say by flooding social media with fake news, but something beyond that — that the technology itself would see us as an enemy or at least as an inferior being to be controlled, with or without our knowledge. One who was deeply concerned about such a dystopia was, for example, the famous author Dr. Yuval Noah Harari, who in an article for the <a href="https://www.ynet.co.il/digital/technology/article/r1xp0i143">Israeli online paper YNet</a> wrote thus:</p><blockquote>The artificial intelligence tools that have appeared in recent years threaten the survival of human civilization from an unexpected direction. Tools such as ChatGPT have developed extraordinary abilities to understand and create language through words, sounds, or images. Artificial intelligence has cracked the code of the human operating system… What will happen when non-human intelligence is better than the average human at telling stories, composing music, painting pictures, and writing laws, and knows how to exploit human weaknesses and addictions better than any human?</blockquote><p>So it seems that the future is already here — that Asimov’s vision has not yet been realized in us, but that we have here a thick hint that it could be if we so desired. The currently available tools are not yet strong enough to be considered “superintelligence”, probably, but the various tech companies are working intensively to strengthen these tools, and all manner of researchers in the field are predicting that unless we take precautions, the day is not far off when we will indeed create such a superintelligence. And on that day, what would prevent it from beginning to manipulate the world behind the scenes, in a way that none of us would even be aware that it was doing so? And who knows what its motivation would be on that day? Would it be an ally, or a foe?</p><p>Harari is one of those who is at the far end of the spectrum in terms of the level of concern he has about these developments. There are computer scientists, like Yann LeCun, who<a href="https://www.lepoint.fr/sciences-nature/yuval-harari-sapiens-versus-yann-le-cun-meta-on-artificial-intelligence-11-05-2023-2519782_1924.php#11"> believe that Harari is overstating the case</a>, and that we have the knowledge and the ability to control these sophisticated systems. But of course even more relaxed individuals like Yann agree that they are powerful tools, capable of doing much damage through such things as fake news and the like, and certainly we need to be careful about which systems we give them access to.</p><p>But what intrigued me when I first became interested in this discussion was the very name given to the whole area: Artificial <strong>Intelligence</strong>. Many of the researchers who are leading the AI revolution speak freely of it as an “intelligence”, a “mind”, and emphasize the technological progress that is enabling these programs to become, slowly but surely, smarter than humans. Humans are losing to such programs at chess and at the more complex game of Go, and these are only the more famous examples of how gradually man is losing his status as the smartest creature on earth. In a post I read online, it was written that when GPT4 was administered an IQ test, <a href="https://www.scientificamerican.com/article/i-gave-chatgpt-an-iq-test-heres-what-i-discovered/">it scored 155</a>, which on the accepted scale means that it is considered equivalent to an “extremely gifted” human. And what will be with GPT5? or GPT15?</p><p>And yet, despite these impressive achievements, something bothered me about this. I wanted to know — are these machines really “intelligent”, or is it all just a label that is being used because it is cool?</p><p>In the upcoming posts we will explore this question in depth. For it turns out that whether or not it is intelligence or not has quite broad implications on how we approach the use of them and their promotion in society. For instance, Harari, in the same article in Ynet, believes that these tools are actually intelligent. “We have just encountered an alien intelligence, right here on Earth”, he declares emphatically. “We know little about this intelligence, except that it may destroy our civilization”. He seems to think of artificial intelligence as an extraterrestrial of supreme technological prowess who has landed here on our little blue globe, and therefore there is much to be concerned about. On the other hand, if it is not a matter of intelligence, but only a tool we control that has gotten a power boost, then perhaps things are less alarming, and will not lead to the social and political upheaval that he fears.</p><p>This series is a work-in-progress. But as I see it now, I’d like to start to build the case of those who proclaim definitively that it is an intelligence of sort. Having established their position— and they have very persuasive arguments, I must say — I will present the matter from the opposite angle, and leave you, the listeners, to judge.</p><p>Perhaps the first person to ask the question “Can machines think?” in an academic setting was Alan Turing, and if he was not the first, he was certainly the most famous. Named after him is the famous Turing Test, which was published, surprisingly, in the same year as Asimov’s book, 1950. This test was designed to offer a way of settling the question we posed — can machines think? So it seems to me that the best place to start is there, in the fevered brain of that fascinating progenitor of computer science. So join me here next time where we delve into <a href="https://medium.com/@benshafat/skynet-journeys-2-the-turing-test-235b8459551d">the story of Turing, his test and its implications, which still impact AI research till this day</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e27b79bfa659" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DFS your code, Dufus :)]]></title>
            <link>https://medium.com/@benshafat/dfs-your-code-dufus-b43d5d1f0348?source=rss-2c6f985715bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/b43d5d1f0348</guid>
            <category><![CDATA[code-structure]]></category>
            <category><![CDATA[code-style]]></category>
            <category><![CDATA[code-smells]]></category>
            <category><![CDATA[dfs]]></category>
            <dc:creator><![CDATA[Elisha Rosensweig]]></dc:creator>
            <pubDate>Mon, 08 May 2023 23:29:18 GMT</pubDate>
            <atom:updated>2023-05-09T07:26:25.426Z</atom:updated>
            <content:encoded><![CDATA[<h3>DFS your code, Dufus :)</h3><p>You know how it is: after working for a month on your project, you look at your code and it’s a mess. If you followed my advice from a <a href="https://medium.com/@benshafat/protip-stay-away-from-coding-sporks-fde4d6feb14b">previous post</a>, you’ve broken it up into functions that are self-contained, higher-level logical steps. In this post I’d like to tackle briefly how to <strong>organize those functions</strong>. It’s a method that I developed a few years ago, and following the same naming style of the <a href="https://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>, I like to call it <em>“DFS, Dufus!”</em></p><h3>What is DFS?</h3><p>While DFS is a very famous algorithm, I’ll spend a few moments introducing it here for those who have not made its acquaintance. DFS stands for Depth First Search, and is part of a family of algorithms that tackle the problem of <strong>graph traversal:</strong> you are given a graph and a starting node, and are asked to traverse all the nodes in the graph. Each node needs to be traversed exactly once.</p><p>The DFS approach to solving this problem is to traverse a single branch of the graph all the way until you reach a leaf (“depth first”), and then backtrack recursively, taking the next sub-tree and repeating. In Python this might look something like this:</p><pre>def dfs(node):<br>  &quot;&quot;&quot;<br>  DFS algorithm. Assumes no cycles in the graph.<br>  &quot;&quot;&quot;<br>  print(node.name) # do something at this node, like print it&#39;s name<br>  for child in node.children:<br>      dfs(child)</pre><p>An example of the transitions you would see in a graph can be shown here, starting from the top node, with the number on each edge indicating the step in the algorithm. As you can see, steps 1–4 head all the way down one path before popping back up to other sub branches, and only when the left sub-tree is completely traversed to we start traversing the right one (transition 6 and on).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CMiJy-V8IfYez8PzuleybA.png" /></figure><h3>But — what’s DFS got to do with my code?</h3><p>Great question! Well, it turns out that DFS has a nice property, that child nodes are traversed in proximity with their parents, as you can see above. If you imagine each function as a node, we would like to have related functions close together.</p><p>Let’s take a look at some code and see this in action. Specifically, try to figure out what this code does:</p><pre>import json<br>import random<br>import pandas as pd<br>from pathlib import Path<br><br><br>def randomize_percentiles():<br>    &quot;&quot;&quot;<br>    return three percentiles, one from each third of the 0-1 range.<br>    &quot;&quot;&quot;<br>    rand_percentiles = [random.random()/3,<br>                        random.random()/3 + 0.33,<br>                        random.random()/3 + 0.66]<br>    return [round(p, 2) for p in rand_percentiles]  # round to 2 percentile points<br><br><br>def get_columns(config):<br>    &quot;&quot;&quot;<br>    Return list of columns to compute stats for. If single column, wrap in list.<br>    &quot;&quot;&quot;<br>    if isinstance(config[&#39;columns&#39;], str):<br>        return [config[&#39;columns&#39;]]<br>    return config[&#39;columns&#39;]<br><br><br>def get_dirs(config_json):<br>    return config_json[&#39;dirs&#39;][&#39;input&#39;], config_json[&#39;dirs&#39;][&#39;output&#39;]<br><br><br>def compute_stats(df, config):<br>    percentiles = choose_percentiles(config)<br>    cols = get_columns(config)<br>    df = df[cols]<br>    stats_df = df.describe(percentiles=percentiles)<br>    return stats_df<br><br><br>def choose_percentiles(config):<br>    if config.get(&#39;percentiles&#39;, None) is None:<br>        return [0.1, 0.25, 0.5, 0.75, 0.9]<br>    elif config[&#39;percentiles&#39;] == &#39;random&#39;:<br>        return randomize_percentiles()<br>    else:<br>        return config[&#39;percentiles&#39;]<br><br><br>def run_program(config_path: str):<br>    config = json.loads(config_path)<br>    input_dir, output_dir = get_dirs(config)<br>    df = pd.read_csv(Path(input_dir) / &#39;data.csv&#39;)<br>    stats_df = compute_stats(df, config)<br>    stats_df.to_csv(Path(output_dir) / &#39;stats.csv&#39;)<br><br><br>if __name__ == &#39;__main__&#39;:<br>    config_path = &#39;some/path.json&#39;<br>    run_program(config_path=config_path)</pre><p>The code above is quite trivial: it loads a DF from a CSV file, computes stats about select columns, and then saves the stats. The user provides a path to a JSON config file where the information needed for the program is stored — paths and percentiles to compute. If no percentiles are explicitly stated, the stats are either a default set, or three random ones.</p><p>However, despite being short and simple, it’s quite difficult to follow, because the functions are not in any logical order. Now compare that experience with reading the same code, just with the functions rearranged:</p><pre>import json<br>import random<br>import pandas as pd<br>from pathlib import Path<br><br><br>def run_program(config_path: str):<br>    config = json.loads(config_path)<br>    input_dir, output_dir = get_dirs(config)<br>    df = pd.read_csv(Path(input_dir) / &#39;data.csv&#39;)<br>    stats_df = compute_stats(df, config)<br>    stats_df.to_csv(Path(output_dir) / &#39;stats.csv&#39;)<br><br><br>def get_dirs(config_json):<br>    return config_json[&#39;dirs&#39;][&#39;input&#39;], config_json[&#39;dirs&#39;][&#39;output&#39;]<br><br><br>def compute_stats(df, config):<br>    percentiles = choose_percentiles(config)<br>    cols = get_columns(config)<br>    df = df[cols]<br>    stats_df = df.describe(percentiles=percentiles)<br>    return stats_df<br><br><br>def choose_percentiles(config):<br>    if config.get(&#39;percentiles&#39;, None) is None:<br>        return [0.1, 0.25, 0.5, 0.75, 0.9]<br>    elif config[&#39;percentiles&#39;] == &#39;random&#39;:<br>        return randomize_percentiles()<br>    else:<br>        return config[&#39;percentiles&#39;]<br><br><br>def randomize_percentiles():<br>    &quot;&quot;&quot;<br>    return three percentiles, one from each third of the 0-1 range.<br>    &quot;&quot;&quot;<br>    rand_percentiles = [random.random()/3,<br>                        random.random()/3 + 0.33,<br>                        random.random()/3 + 0.66]<br>    return [round(p, 2) for p in rand_percentiles]  # round to 2 percentile points<br><br><br>def get_columns(config):<br>    &quot;&quot;&quot;<br>    Return list of columns to compute stats for. If single column, wrap in list.<br>    &quot;&quot;&quot;<br>    if isinstance(config[&#39;columns&#39;], str):<br>        return [config[&#39;columns&#39;]]<br>    return config[&#39;columns&#39;]<br><br><br>if __name__ == &#39;__main__&#39;:<br>    config_path = &#39;some/path.json&#39;<br>    run_program(config_path=config_path)</pre><p>Much simpler to follow, right? Why is that? The reason is twofold:</p><ul><li>First, the functions are organized in such a way that it’s easy to read the code from top to bottom, without needing to move up and down the code much.</li><li>Second, when a function X calls another function Y, the two functions are close together and can be located and read with ease, like two consecutive paragraphs in a book.</li></ul><p>How did we manage this, you ask? DFS, dufus! To see this, let us sketch in graph form the functions. If function X calls function Y, X will have an edge leading to Y, and will be drawn as the parent node of Y. Then, let’s add numbers indicating <strong>the order in which the functions would be called</strong> in a standard run of the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*vanryllwvDj-3_8v7Riblw.png" /></figure><p>In this example, we have a function like <em>compute_stats</em>, which calls two functions. The first is <em>choose_percentiles, </em>so we position it right after <em>compute_stats. </em>We then position all the functions <em>choose_percentiles</em> calls, using the same logic recursively. Only once we’re done there, do we position <em>get_columns, </em>the second function called by<em> compute_stats.</em></p><p>If you follow the restructured code above, you will see that the numbers match that of what DFS would produce on this graph. This matches the intuition we mentioned earlier: with DFS, child nodes are traversed in proximity with their parents. In code reading terms, DFS ordering will <strong>position code segments close to related code segments</strong>, making it easier to comprehend and follow.</p><h3>Some final words</h3><p>What we saw here is how a simple rule-of-thumb can help make your code much more readable and easy to follow. Still, we did not demonstrate the more complex case when a function is called from <strong>more than one</strong> place. As with actual DFS, the solution here is to position the function according to the first time you reach it in the order of function calls. Indeed, some code might be too complex for the DFS approach to make it readable, but I’ve not found a better approach to-date.</p><p>Finally, one last tip: it’s best to do this type of restructuring only once you have a good sense of what your code will look like. Like most refactoring efforts, it’s best doing it when the content and essence of your code is solid in your mind, and you know your refactoring will be long-lasting.</p><p>Anyway — I hope you give it a try! If it helps, adopt it. If it does not work for you, or you have your own system — please share :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b43d5d1f0348" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ProTip: Stay away from Coding Sporks]]></title>
            <link>https://medium.com/@benshafat/protip-stay-away-from-coding-sporks-fde4d6feb14b?source=rss-2c6f985715bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/fde4d6feb14b</guid>
            <category><![CDATA[code-smells]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[encapsulation]]></category>
            <category><![CDATA[code-style]]></category>
            <dc:creator><![CDATA[Elisha Rosensweig]]></dc:creator>
            <pubDate>Wed, 19 Apr 2023 22:14:15 GMT</pubDate>
            <atom:updated>2023-09-21T12:32:35.406Z</atom:updated>
            <content:encoded><![CDATA[<p>Are you familiar with the Spork? At least among hikers, this handy item is something they have likely come across once or twice. It’s a single tool that can function as any of the three main cutlery utensils — a spoon, fork, or knife.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*1gTqv7dT2nvv94-gJv2A7Q.png" /><figcaption>I, Spork.</figcaption></figure><p>While cool, it has its drawbacks. For one thing, you can only use one of the three utensils at a time, so the common knife-fork combo is not an option. Also, the restrictions placed by the need to combine it all into a single utensil warps the individual tools. The knife is quite small, and when you use the fork you risk pricking your mouth on the serrated side. These properties make the spork ok as Plan B but not a great Plan A. I tend to pack one just in case, but my backpack will also carry the usual utensil trio, which is what I use 9/10 times.</p><p>So, that’s the analogy — now let’s move on to what it’s an analogy for, shall we?</p><p>In my career in R&amp;D, I’ve spent time in both R and D sides of the aisle. One of the things I’ve noticed is how researchers tend to write the “spork” code — long sequential scripts dedicated and optimized to the specific task at hand. Give that code to a software engineer, however, and they will suggest to break the code into a set of small “lego blocks” that can be put together and used in multiple ways, where the current use is just one example. Researchers, from my experience, will most times shrug, unconvinced. They will say that the code is there to analyze data, and if the data analysis works out fine, it’s a waste of time to obsess about the structure of the code.</p><p>In what follows, I’ll try to convince the researchers in the audience that the engineers are on to something here. To do this, I’ll first walk you through an example of how to take a spork-ish code and modify it to be more lego-like. Then, I’ll demonstrate why the latter is better — better for the code, and better for the researcher.</p><h3>Presenting: A Snippet of Spork Coding</h3><p>Let’s take a look at the following snippet of spork-ish code. For the purpose of this exercise, I took a segment of code from a Kaggle notebook found online, modified much of it while keeping the basic structure in place.</p><pre>import pandas as pd<br>import seaborn as sns<br>import matplotlib.pyplot as plt<br>from catboost import CatBoostClassifier<br>from sklearn.model_selection import cross_val_score, StratifiedKFold<br>import shap<br><br>train = pd.read_csv(&quot;/data/train.csv&quot;)<br><br>def feat_analysis(target, plottype):<br>    train_c = train.copy()<br>    train_c[f&quot;{target}_binary&quot;] = train_c[&quot;label&quot;].apply(lambda x: 1 if x == target else 0)<br><br>    features = [col for col in train.columns if col not in [&quot;label&quot;, f&quot;{target}_binary&quot;]]<br><br>    model = CatBoostClassifier(random_state=101, silent=True)<br>    model.fit(train_c[features], train_c[f&quot;{target}_binary&quot;])<br>    skf = StratifiedKFold(n_splits=4, shuffle=True, random_state=50)<br><br>    cv_score = cross_val_score(CatBoostClassifier(random_state=1, silent=True),<br>                               X=train_c[features], y=train_c[f&quot;{target}_binary&quot;],<br>                               scoring=&quot;roc_auc&quot;, cv=skf).mean()<br><br>    if plottype == &quot;importance&quot;:<br><br>        importances = model.feature_importances_<br>        indices = importances.argsort()[::-1]<br>        selected_features = [features[i] for i in indices][::-1]<br>        importances = importances[indices][::-1]<br><br>        plt.figure(figsize=(16, 9))<br>        plt.bar(range(len(importances)), importances,<br>                color=sns.light_palette(&quot;seagreen&quot;, <br>                n_colors=len(importances)),)<br><br>        plt.yticks(range(len(importances)), selected_features)<br>        plt.xlabel(&quot;Feat. Importance&quot;)<br>        plt.title(f&quot;Feature Importance of {target}, score = {cv_score}&quot;)<br>        plt.show()<br><br>    elif plottype == &quot;shap&quot;:<br><br>        shap.initjs()<br>        explainer = shap.TreeExplainer(model)<br>        shap_values = explainer.shap_values(train[features])<br>        shap.summary_plot(shap_values, features=train[features],<br>                          feature_names=train[features].columns,<br>                          max_display=len(train[features].columns),<br>                          )<br><br><br>sicknesses = list(train_df.prognosis.unique())<br><br>feat_analysis(sicknesses[0], &quot;importance&quot;)<br>feat_analysis(sicknesses[0], &quot;shap&quot;)<br></pre><p>The code above follows a standard flow of data analysis, so we will add some comments indicating the different steps. While we’re at it, let’s add the variable train to the function signature of feat_analysis. This will be helpful since <strong>(a)</strong> we can rid ourselves of using train_c in the function; <strong>(b)</strong> we can move reading the data down to the end of the file, with the rest of the execution flow; and most importantly - <strong>(c)</strong> to rid ourselves of the <a href="https://stackoverflow.com/questions/19158339/why-are-global-variables-evil">evil global variable</a>.</p><p>After these changes we get:</p><pre>import pandas as pd<br>import seaborn as sns<br>import matplotlib.pyplot as plt<br>from catboost import CatBoostClassifier<br>from sklearn.model_selection import cross_val_score, StratifiedKFold<br>import shap<br><br><br>def feat_analysis(train, target, plottype):<br>    train = train.copy()  # don&#39;t change the original<br><br>    # data munging / wrangling<br>    train_c[f&quot;{target}_binary&quot;] = train[&quot;label&quot;].apply(lambda x: 1 if x == target else 0)<br><br>    # feature selection<br>    features = [col for col in train.columns if col not in [&quot;label&quot;, f&quot;{target}_binary&quot;]]<br><br>    # model building and evaluation<br>    model = CatBoostClassifier(random_state=101, silent=True)<br>    model.fit(train[features], train[f&quot;{target}_binary&quot;])<br>    skf = StratifiedKFold(n_splits=4, shuffle=True, random_state=50)<br><br>    cv_score = cross_val_score(CatBoostClassifier(random_state=1, silent=True),<br>                               X=train[features], y=train[f&quot;{target}_binary&quot;],<br>                               scoring=&quot;roc_auc&quot;, cv=skf).mean()<br>    <br>    # plotting<br>    if plottype == &quot;importance&quot;:<br><br>        importances = model.feature_importances_<br>        indices = importances.argsort()[::-1]<br>        selected_features = [features[i] for i in indices][::-1]<br>        importances = importances[indices][::-1]<br><br>        plt.figure(figsize=(16, 9))<br>        plt.bar(range(len(importances)), importances,<br>                color=sns.light_palette(&quot;seagreen&quot;, <br>                n_colors=len(importances)),)<br><br>        plt.yticks(range(len(importances)), selected_features)<br>        plt.xlabel(&quot;Feat. Importance&quot;)<br>        plt.title(f&quot;Feature Importance of {target}, score = {cv_score}&quot;)<br>        plt.show()<br><br>    elif plottype == &quot;shap&quot;:<br><br>        shap.initjs()<br>        explainer = shap.TreeExplainer(model)<br>        shap_values = explainer.shap_values(train[features])<br>        shap.summary_plot(shap_values, features=train[features],<br>                          feature_names=train[features].columns,<br>                          max_display=len(train[features].columns),<br>                          )<br><br># get dataset<br>train = pd.read_csv(&quot;/data/train.csv&quot;)<br><br># lets run it!<br>sicknesses = list(train.prognosis.unique())<br><br>feat_analysis(train=train, target=sicknesses[0], plottype=&quot;importance&quot;)<br>feat_analysis(train=train, target=sicknesses[0], plottype=&quot;shap&quot;)</pre><p>Next, to the main event: let’s extract some sub-functions out of the main code, one such function for each of the sections that we added a comment for — munging, feature selection etc. These are going to be our lego pieces:</p><pre>import pandas as pd<br>import seaborn as sns<br>import matplotlib.pyplot as plt<br>from catboost import CatBoostClassifier<br>from sklearn.model_selection import cross_val_score, StratifiedKFold<br>import shap<br><br><br>def feat_analysis(train, target, plottype):<br>    train = train.copy()  # don&#39;t change the original<br><br>    munge_data(target, train)<br>    features = select_features(target, train)<br>    model, cv_score = fit_and_evaluate(features, target, train)<br><br>    if plottype == &quot;importance&quot;:<br>        plot_importance(model, features, cv_score, target)<br><br>    elif plottype == &quot;shap&quot;:<br>        plot_shap(model, features, train, target)<br><br><br>def munge_data(target, train):<br>    train[f&quot;{target}_binary&quot;] = train[&quot;label&quot;].apply(lambda x: 1 if x == target else 0)<br><br><br>def select_features(target, train):<br>    features = [col for col in train.columns if col not in [&quot;label&quot;, f&quot;{target}_binary&quot;]]<br>    return features<br><br><br>def fit_and_evaluate(features, target, train):<br>    model = CatBoostClassifier(random_state=101, silent=True)<br>    model.fit(train[features], train[f&quot;{target}_binary&quot;])<br>    skf = StratifiedKFold(n_splits=4, shuffle=True, random_state=50)<br><br>    cv_score = cross_val_score(CatBoostClassifier(random_state=1, silent=True),<br>                               X=train[features], y=train[f&quot;{target}_binary&quot;],<br>                               scoring=&quot;roc_auc&quot;, cv=skf).mean()    <br><br>    return model, cv_score<br><br><br>def plot_importance(model, features, cv_score, target):<br>    importances = model.feature_importances_<br>    indices = importances.argsort()[::-1]<br>    selected_features = [features[i] for i in indices][::-1]<br>    importances = importances[indices][::-1]<br><br>    plt.figure(figsize=(16, 9))<br>    plt.bar(range(len(importances)), importances,<br>            color=sns.light_palette(&quot;seagreen&quot;, <br>            n_colors=len(importances)))<br><br>    plt.yticks(range(len(importances)), selected_features)<br>    plt.xlabel(&quot;Feat. Importance&quot;)<br>    plt.title(f&quot;Feature Importance of {target}, score = {cv_score}&quot;)<br>    plt.show()<br><br><br>def plot_shap(model, features, train, target):<br>    shap.initjs()<br>    explainer = shap.TreeExplainer(model)<br>    shap_values = explainer.shap_values(train[features])<br>    shap.summary_plot(shap_values, features=train[features],<br>                      feature_names=train[features].columns,<br>                      max_display=len(train[features].columns))<br><br><br># get dataset<br>train = pd.read_csv(&quot;/data/train.csv&quot;)<br><br># lets run it!<br>sicknesses = list(train.prognosis.unique())<br><br>feat_analysis(train=train, target=sicknesses[0], plottype=&quot;importance&quot;)<br>feat_analysis(train=train, target=sicknesses[0], plottype=&quot;shap&quot;)</pre><p>Let’s take a moment to consider this version, when all we did was to copy the existing code into dedicated functions. Our new version is much improved in multiple ways!</p><h4>Impact #1: Easy to follow &amp; orient</h4><p>In the original version of the code, the solitary function was 40+ lines long, and you had to read it carefully to figure out what it was doing. In the new version, reading only 12 lines gives you a clear and concise sense of what is going on:</p><pre>def feat_analysis(train, target, plottype):<br>    train = train.copy()  # don&#39;t change the original<br><br>    munge_data(target, train)<br>    features = select_features(target, train)<br>    model, cv_score = fit_and_evaluate(features, target, train)<br><br>    if plottype == &quot;importance&quot;:<br>        plot_importance(model, features, cv_score, target)<br><br>    elif plottype == &quot;shap&quot;:<br>        plot_shap(model, features, train, target)<br></pre><p>The importance of this improved readability alone cannot be overrated.</p><ul><li>It’s very easy to understand what the code does even if you never saw it before. We even removed the comments we had added before — the function names are so clear, the reader does not need additional cues to understand what’s happening.</li><li>It’s easy to navigate to the portion of the code you are interested in (e.g., “where can I find the place where features are selected?”)</li><li>If the code crashes due to a bug, it’s easy to figure out by the enclosing function in which phase of the pipeline the bug is (e.g., “building the model is causing a problem”). It can also help in deciding where to place debug prints and breakpoints.</li></ul><h4>Impact #2: Easy to update &amp; modify</h4><p>Improving the readability of the code also enables you to quickly update and modify the code when needed. This is due to the fact that, since sections of the code are in their own functions, you know exactly what each variable is used for, and what it can affect. There is less room for concern that one change will ripple far and wide, unnoticed till it’s too late.</p><h4>Impact #3: Easy to track information flow</h4><p>Each function you extract has a <em>signature</em>, which allows us to see what information each phase uses. For example, it’s easy to see just from the signatures that target is used also when extracting features, which makes sense — you don’t want to use your dependent variable as a feature. Seeing that in the signature of the function can help you be secure the code makes sense.</p><p>In other words, externalizing the information each phase uses gives us a high-level view of the logic we’re applying, and help us zoom in on suspicious components as well.</p><h4>Impact #4: Code Reuse and Extendibility</h4><p>The final major benefit of this restructure is that each of these blocks of code are now <em>reusable anywhere in your code base</em>. For example, let us consider the last function we created:</p><pre>def plot_shap(model, features, train, target):<br>    shap.initjs()<br>    explainer = shap.TreeExplainer(model)<br>    shap_values = explainer.shap_values(train[features])<br>    shap.summary_plot(shap_values, features=train[features],<br>                      feature_names=train[features].columns,<br>                      max_display=len(train[features].columns))</pre><p>When looking at this function in isolation, it’s clear that there is very little (if anything) in this code that is specialized for this specific dataset or analysis. Rather, it would be quite nice if this plotting logic would be available to you and others when using SHAP values in other projects.</p><p>By extracting the logic into its own coding block and wrapping it into a function, you also turn it into a standalone tool. Then, due to its more narrow focus, it can actually be used much more broadly (just as a fork is useful in more cases than a Spork precisely because it has less capabilities).</p><p>To state this more generally: By breaking the code into multiple sub-functions, you are changing your code from a single combo-tool that can only be used in this project (= Spork) to building an entire toolbox filled with dedicated tools that can be used broadly (= standard utensils).</p><h3>Summary, and: how important is this really?</h3><p>In this post we focused on a single change to the code: <strong>breaking it into self-contained logical chunks</strong>. We saw how even this small change enables us to work better and smarter. The ROI of this change is quite high, and requires very little effort.</p><p>Well — usually. In some cases you might find that breaking up the code into such chunks is complicated. If that’s the case, and you’re in a time crunch, then sure — don’t spend time on it right away. However, when you have such “spaghetti code” that is not easily broken up, it’s usually a sign that your code is very hard to follow and manage, and that will come back to bite one day.</p><p>In other words, with such code you’re likely to find yourself in the future banging your head against the wall wishing you had cleaned it up when you knew what it was all about. So, if you find that it’s not easy to make even this small change, consider it a big warning sign. Thus, even a mental exercise trying to see if you <em>could</em> do such a change easily will help you evaluate where you are at risk.</p><p>It’s a well-known adage that “<a href="https://devblogs.microsoft.com/oldnewthing/20070406-00/?p=27343">code is read much more than it is written</a>”, and in many cases that future reader of your code is you — six months from now, when you don’t remember anymore what you were doing exactly. So — take care of your future self!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*zSXJeE6gK3trNZbLugYZhw.png" /><figcaption>Source: StackOverflow (<a href="https://shorturl.at/bgzW7">https://shorturl.at/bgzW7</a>)</figcaption></figure><p>Finally: Buy a Spork. It’s not good for much, but it’s still cool to have one around, just in case… :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fde4d6feb14b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>